Authorize YouTube Data API for Android through OAuth2

I am trying to allow OAuth2 to use the YouTube data API. I want to write a simple client for Android to be able, for example, to get lists of user subscriptions, a playlist, etc. The problem is that every method I try (for example, HTTP requests like https://www.googleapis.com/youtube/v3/subscriptions or YouTube library com.google.apis:google-api-services-youtube:v3-rev120-1.19.0 ), I get the same error:

401 Invalid credentials

I am doing the following steps:

  • Register the application in the Google console. Enable YouTube Data API. Create an OAuth credential with my application package name and keystore.
  • Create an Android API key.

And here I have a lot of questions.

  • To be able to receive subscriptions, etc., do I need to register an application with OAuth2 credentials and (?) Register API Key?
  • How can I use client_id and other data that are in client_secrets.json? How can I use this client_secrets.json? Do I need to download it and how to use it?

Because when I saw an example code, for example:

 GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context.getApplicationContext(), Arrays.asList(SCOPES)); credential.setSelectedAccountName(accountName); YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName(context.getString(R.string.app_name)).build(); 

Or use AccountManager and get a token

  accountManager.getAuthToken(userAccount, "oauth2:" + mScope, null, (Activity) mContext, new OnTokenAcquired(), null); where mScope="https://gdata.youtube.com" 

and then use it that way

 https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&maxResults=10&mine=true&key={here is my key}&access_token= ya29.bgJa5J8LHHu0Mu3P87mbPY9gfhSFzWUHh8gCw022FC6yfMO6GfVHveYr_BG1-HnWF-jpd-IDcGGVrF3gEf4UhSHPSrV76RAwqd4V17ixn72s1Xl6a0wQ4FVDz3cEZjUEN80o 

I get the same authError error every time (shorter):

"code": 401,
"message": "Invalid credentials"

What am I doing wrong? How to use client_id in options 1 and 2. Is it necessary? Because I think this is the reason why this does not work.

+5
source share

Source: https://habr.com/ru/post/1240946/


All Articles