android - YouTube.Builder 的正确使用方法

标签 android scala youtube-api

我需要获取用户 youtube 视频。这是我的代码,它只尝试获取用户 youtube channel 。但它不起作用 - 选择帐户后,loadYoutubeChannels 总是报错。我读了这个answer还有这个yt-direct-lite-android ,但仍然不明白为什么他们没有得到错误,但我得到了。
这是我的代码:

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"

}

当我不使用 ApiKey 时,我的意思是当 (1)(2) 被注释时,我得到这个错误:

{
  "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."
}

当我使用浏览器 key api key 时,我得到这个错误:

{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}

据我所知,yt-direct-lite-android 仅在 YouTubePlayerFragment 中使用 ApiKey,但在获取一些 youtube 数据时不使用 ( channel 等)。所以..这是不公平的。

有人能告诉我做错了什么吗?谢谢。

最佳答案

这是我的解决方案:

打开google developers console
转到凭据/OAuth 同意屏幕。填写向用户显示的产品名称。也把它放在这里:

service = new YouTube.Builder(transport, factory, credential)
  .setApplicationName(/*Product name shown to users*/)

下一步 - 注册您的应用。转到 Credentials/credentials 并单击按钮 Create credentials 然后选择 OAuth Client ID 然后选择 Android 并将您的在那里应用 SHA1。

最后一步 - 创建 api key 。在 Credentials/credentials 中单击按钮 Create credentials 并选择 API key,然后选择 Browser key。然后在这里使用这个生成的浏览器 key 。

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

关于android - YouTube.Builder 的正确使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582274/

相关文章:

scala - Apache Spark - 数据集操作在抽象基类中失败?

scala - 从特定列Scala Spark数据帧获取最小值和最大值

javascript - 如何在更改选择列表中的选项时停止所有 YouTube 视频

java - 停止Android音频播放器的背景声音

java - 在第一个和第三个 Activity 之间传递数字

scala - 使用 Either 处理 Scala 代码中的故障

youtube - 如何使用 Flash builder 4.6 在 youtube chromeless 播放器上显示自定义播放器控件?

.net - 从嵌入的 YouTube 视频中删除品牌和标题链接

java - 如何使用指数因子格式化十进制数?

android - 粘性事件应该在应用程序关闭后保留 GreenRobot Eventbus