Using Google play services with youtube

2019-04-02 18:57发布

I am trying to use the new Youtube API V3 : https://developers.google.com/youtube/v3/ in my android app.

To login the user to youtube services, I chose the new Google Play Services : http://developer.android.com/google/play-services/index.html

Here is my code :

AccountManager manageurcomptes = AccountManager.get(ActiviteGeneral.this);
Account[] comptes = manageurcomptes
                        .getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String jeton;
try {
    jeton = GoogleAuthUtil.getToken(ActiviteGeneral.this, comptes[0].name,
        "audience:server:client_id:xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com");
    int i = 0;
} catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
} catch (GoogleAuthException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();

For the GoogleAuthUtil.getToken scope parameter, I followed the article from Android Developper Blog : "Verifying Back-End Calls from Android Apps" http://android-developers.blogspot.fr/2013/01/verifying-back-end-calls-from-android.html and registered my app on Google API Console.

I created a "Client ID for installed applications".

The result is a GoogleAuthException with "detailMessage" = "Unknown".

Where is the mistake ?

1条回答
淡お忘
2楼-- · 2019-04-02 19:05

I made an example that shows how can access to the YouTube API v3 from an Android app that works very well:

http://lookthiscode.blogspot.com.ar/2013/01/utilizando-youtube-data-api-v3-desde.html

Actually this example is based in the official example that shows how to use the task API from an Android app. I modified the example to use the YouTube API authentication scope.

The article is in spanish but you can download the source code of the project using the link at the end of the article.

The main piece of code to solve your problem is the following:

// Google Accounts
credential = GoogleAccountCredential.usingOAuth2(this, YouTubeScopes.YOUTUBE, YouTubeScopes.YOUTUBE_READONLY);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// YouTube client
service =
        new com.google.api.services.youtube.YouTube.Builder(transport, jsonFactory, credential)
            .setApplicationName("Google-YouTubeAndroidSample/1.0").build();
查看更多
登录 后发表回答