2017-06-11 1 views
0

J'ai suivi this article pour implémenter Google Sign-In dans une application Android.Utilisez Google Connexion pour autoriser YouTube API de données

Ce dernier bit de code nous donne un objet GoogleSignInAccount qui contient des informations sur l'utilisateur signé en:

private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     GoogleSignInAccount acct = result.getSignInAccount(); 
     mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName())); 
     updateUI(true); 
    } else { 
     // Signed out, show unauthenticated UI. 
     updateUI(false); 
    } 
} 

Dans tous les code samples provided pour l'API de données YouTube, le code d'authentification suivant est utilisé:

public static Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = ApiExample.class.getResourceAsStream("/client_secret.json"); 
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

    // Build flow and trigger user authorization request. 
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
     .setDataStoreFactory(DATA_STORE_FACTORY) 
     .setAccessType("offline") 
     .build(); 
    Credential credential = new AuthorizationCodeInstalledApp(
    flow, new LocalServerReceiver()).authorize("user"); 
    System.out.println(
     "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
    return credential; 
} 

maintenant parce que je l'ai déjà authentifié à l'aide de Google Sign-In, comment puis-je créer un objet Credential en utilisant l'objet GoogleSignInAccount pour passer à YouTube.Builder?

+0

Je vais écrire une réponse à cette question plus tard, mais en attendant, [ce] (https://gist.github.com/bertrandmartel/7d323b09af889f5c03b862612c796046) m'a aidé à résoudre mon problème. – sudoman

Répondre

0
public static final Collection<String> YOUTUBE_SCOPE = Arrays.asList("https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtubepartner"); 

... 

private void handleSignInResult(GoogleSignInResult result) { 

    ... 

    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, YOUTUBE_SCOPE); 
    credential.setSelectedAccount(acct.getAccount()); 

    ... 

} 

Source