How to get access token from GoogleCredential?

后端 未结 2 1280
感情败类
感情败类 2020-12-08 04:38

I am trying to get an access token to use the Google Play Android Developer API, and I got this far using the Google API Java Client documentation example:

H         


        
相关标签:
2条回答
  • 2020-12-08 05:38

    If you use ServiceAccountCredentials.fromStream() to create your credential object, you may have to call the createScoped() method too.

        FileInputStream fileInputStream = new FileInputStream(KEY_FILE_PATH);
        GoogleCredentials credentials = ServiceAccountCredentials.fromStream(fileInputStream);
        credentials = credentials.createScoped(SCOPES);
        AccessToken accessToken = credentials.refreshAccessToken();
    

    Otherwise you get the error

    java.io.IOException: Scopes not configured for service account. Scoped should be specified by calling createScoped or passing scopes to constructor.

    0 讨论(0)
  • 2020-12-08 05:39

    Got it. You have to call credential.refreshToken() before credential.getAccessToken(). It doesn't say this anywhere in the documentation but that's what does it.

    credential.refreshToken();
    accessToken = credential.getAccessToken();
    
    0 讨论(0)
提交回复
热议问题