【问题标题】:Get List of Youtube channels subscribe by current user in android?获取当前用户在 android 中订阅的 Youtube 频道列表?
【发布时间】:2017-01-15 08:32:00
【问题描述】:

您好,我想获取当前用户在 android 中订阅的频道列表。

我遵循的步骤:

1) https://developers.google.com/youtube/v3/docs/subscriptions/list

通过阅读描述,我在 google 开发者控制台中创建了 API 密钥oauth 密钥并厌倦了实现以下请求

GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mine=true&key={YOUR_API_KEY}。你可以看到我只设置了part = snippetmine = true

为了验证我关注了https://developer.android.com/training/id-auth/authenticate.html

这是我的代码

AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();

am.getAuthToken(
    myAccount_,                     // Account retrieved using getAccountsByType()
    "Manage your tasks",            // Auth scope
    options,                        // Authenticator-specific options
    this,                           // Your activity
    new OnTokenAcquired(),          // Callback called when a token is successfully acquired
    new Handler(new OnError()));    // Callback called if an error occurs

添加适当的权限后,我可以在 myAccount_ 中获取我的帐户

关于TokenAcquire

的回调
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        // Get the result of the operation from the AccountManagerFuture.
        Bundle bundle = result.getResult();

        // The token is a named value in the bundle. The name of the value
        // is stored in the constant AccountManager.KEY_AUTHTOKEN.
        token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        ...
    }
}

我没有获得令牌价值

请指导我。 是否有其他方法或者是否必须更新/修改某些内容?

我在 xml 中的权限是

<uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

在我的异常处理捕获中,token = bundle.getString(AccountManager.KEY_AUTHTOKEN); 这一行出现错误。

android.accounts.AuthenticatorException。 尝试在空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String)”

我使用的是 android 6.0,它需要运行时权限。

【问题讨论】:

  • 粘贴完整的错误代码
  • android.accounts.AuthenticatorException
  • 尝试在空对象引用上调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'......
  • token = bundle.getString(AccountManager.KEY_AUTHTOKEN);这里我出错了

标签: unity3d youtube-api android-youtube-api


【解决方案1】:

要根据Obtain an access token成功获取访问令牌,我认为您需要为Youtube API使用'https://gdata.youtube.com'的有效范围。但是,查看您的代码,您似乎将“管理您的任务”作为身份验证范围传递。请尝试将其设置为 'https://gdata.youtube.com'

关于获取订阅者数量,请尝试使用以下 URI GET 请求发出REST call in Android

GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mySubscribers=true&fields=pageInfo&key={SERVER_API_KEY}

它返回一个 JSON 响应,其中 totalResults 表示订阅者的数量:

{
   "pageInfo": {
   "totalResults": 2,
   "resultsPerPage": 5
  }
}

【讨论】:

  • 将我的范围更改为 ''gdata.youtube.com'' 但仍然收到 android.accounts.AuthenticatorException
  • 什么是server_api_key?和 part=sn-p?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 2013-09-09
  • 2019-02-15
  • 1970-01-01
相关资源
最近更新 更多