Android:使用Google登录获取访问令牌

Ily*_*man 5 android google-plus

在读最后谷歌+消息后在这里.完成登录后如何获取访问令牌?

jen*_*nzz 3

您看过API 参考吗?

您可能正在寻找的课程是com.google.android.gms.auth.GoogleAuthUtil.

除其他外,它还提供以下方法:
static String getToken(Context context, String accountName, String

描述:
对用户进行身份验证并返回有效的 Google 身份验证令牌,或者如果获取令牌出错则引发异常。

用法:

String token;
try {
    token = GoogleAuthUtil.getToken(context, accountName, scope);
} catch (GooglePlayServicesAvailabilityException playEx) {
    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
        playEx.getConnectionStatusCode(),
        Activity.this,
        AUTH_REQUEST_CODE);
    // Use the dialog to present to the user.
} catch (UserRecoverableAutException recoverableException) {
    Intent recoveryIntent = recoverableException.getIntent();
    // Use the intent in a custom dialog or just startActivityForResult.
} catch (GoogleAuthException authEx) {
    // This is likely unrecoverable.
    Log.e(TAG, "Unrecoverable authentication exception: " + authEx.getMesssage(), authEx);
} catch (IOException ioEx) {
    Log.i(TAG, "transient error encountered: " + ioEx.getMessage());
    doExponentialBackoff();
}
Run Code Online (Sandbox Code Playgroud)