Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用YouTube.Builder的正确方法_Android_Scala_Youtube Api - Fatal编程技术网

Android 使用YouTube.Builder的正确方法

Android 使用YouTube.Builder的正确方法,android,scala,youtube-api,Android,Scala,Youtube Api,我需要获得用户的youtube视频。这是我的代码,它只尝试获取用户youtube频道。但它不起作用-在选择account之后,loadYoutubeChannels总是显示错误。我读了这个和这个,但仍然不明白为什么他们没有得到错误,但我得到了。 这是我的密码: class YotubeTestActivity extends AppCompatActivity { private var credential: GoogleAccountCredential = _ private

我需要获得用户的youtube视频。这是我的代码,它只尝试获取用户youtube频道。但它不起作用-在选择account之后,loadYoutubeChannels总是显示错误。我读了这个和这个,但仍然不明白为什么他们没有得到错误,但我得到了。
这是我的密码:

class YotubeTestActivity extends AppCompatActivity {

  private var credential: GoogleAccountCredential = _
  private var service: YouTube = _

  override def onCreate(savedInstanceState: Bundle): Unit = {
    super.onCreate(savedInstanceState)

    val prefs = getPreferences(Context.MODE_PRIVATE)
    credential = GoogleAccountCredential.usingOAuth2(this, List(YouTubeScopes.YOUTUBE_READONLY))
    credential.setSelectedAccountName(prefs.getString(PrefAccountName, None.orNull))

    val transport = new NetHttpTransport()
    val factory =  new JacksonFactory()

    service = new YouTube.Builder(transport, factory, credential)
      .setApplicationName(getString(R.string.app_name))
      .setYouTubeRequestInitializer(new YouTubeRequestInitializer(ApiKey)) // <-- (1)
      .build()
  }

  /*
  check for Google Play Service

  choose account
   */

  private def onAccountChooseOk(accountName: String): Unit = {
    credential.setSelectedAccountName(accountName)

    val prefs = getPreferences(Context.MODE_PRIVATE).edit()
    prefs.putString(PrefAccountName, accountName)
    prefs.commit()

    rxScalaRunInBackground(loadYoutubeChannels, onLoadYoutubeChannelsOk, onLoadYoutubeChannelsError)
  }

  private def loadYoutubeChannels(): ChannelListResponse = {
    val channelsQuery = service.channels().list("contentDetails")
    channelsQuery.setMine(true)
    channelsQuery.setMaxResults(50l)
    //channelsQuery.setKey(ApiKey)  // <-- (2)
    channelsQuery.execute()
  }

  private def onLoadYoutubeChannelsError(e: Throwable): Unit = {
    Log.e(Tag, "onLoadYoutubeChannelsError", e)

    e match {
      case _: GooglePlayServicesAvailabilityIOException => // checkGooglePlayServicesAvailable()
      case e: UserRecoverableAuthIOException => // startActivityForResult(e.getIntent, RequestAuthorization);
      case _ => finish()
    }
  }

  private def onLoadYoutubeChannelsOk(response: ChannelListResponse): Unit = {
    Log.e(Tag, s"onLoadYoutubeChannelsOk: $response")
  }

}

object YotubeTestActivity {

  private val Tag = "YotubeTestActivity"    
  private val PrefAccountName = "accountName"    
  private val ApiKey = "API_KAY_FROM_DEV_CONSOLE"

}
使用浏览器键或时,出现以下错误:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Access Not Configured. YouTube Data API has not been used in project 608941808256 bef$re or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube/overview?project=608941808256 then r$try. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "reason" : "accessNotConfigured",
    "extendedHelp" : "https://console.developers.google.com/apis/api/youtube/overview?project=60894180$256"
  } ],
  "message" : "Access Not Configured. YouTube Data API has not been used in project 608941808256 befor$ or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube/overview?project=608941808256 then ret$y. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
}
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}
据我所知,yt direct lite android仅在YouTubePlayerFragment中使用ApiKey,但在获取一些youtube数据(频道等)时不使用ApiKey。所以这不公平

有人能说出我做错了什么吗?谢谢。

这是我的解决方案:

打开
转到凭证/OAuth同意屏幕。填写显示给用户的产品名称。并把它放在这里:

service = new YouTube.Builder(transport, factory, credential)
  .setApplicationName(/*Product name shown to users*/)
下一步-注册你的应用程序。转到凭据/凭据,单击按钮创建凭据,然后选择OAuth客户端ID,然后选择Android并将应用程序SHA1放在那里

最后一步-创建api密钥。在凭据/凭据中,单击按钮创建凭据并选择API密钥,然后选择浏览器密钥。然后在此处使用生成的浏览器键

private val ApiKey = "/*Browser key*/"