Reputation: 607
Hello I want to get list of channels subscribe by current user in android.
Steps I follow:
1) https://developers.google.com/youtube/v3/docs/subscriptions/list
By read the description, I created API key for android and oauth key in google developer console and tired to implement following request
GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mine=true&key={YOUR_API_KEY}. you can see I have set only part = snippet
and mine = true
.
For authentication I followed https://developer.android.com/training/id-auth/authenticate.html
Here is my code
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
After adding proper permission I can get my account in myAccount_
on call backbaack of 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);
...
}
}
I am not getting the token value.
Please guide me. Is there some alternative approach Or if have to update/modify something?
My permission in xml are
<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" />
I am getting error at this line token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
in my catch of exception handling.
android.accounts.AuthenticatorException. Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
I am using android 6.0 and it require run time permission.
Upvotes: 0
Views: 738
Reputation: 17641
To successfully get the access token according to Obtain an access token, I think you need to use a valid scope which is 'https://gdata.youtube.com'
for Youtube API. However, looking at your code, it seems you passed "Manage your tasks" as Auth scope. Try setting that to 'https://gdata.youtube.com'
instead.
With regard to getting number of subscribers, try making a REST call in Android with the following URI GET request:
GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mySubscribers=true&fields=pageInfo&key={SERVER_API_KEY}
It returns a JSON response where totalResults represents the number of subscribers:
{
"pageInfo": {
"totalResults": 2,
"resultsPerPage": 5
}
}
Upvotes: 1