您可能只是在authTokenType前面缺少oauth2:前缀.
此代码有效:
// Note the `oauth2:` prefix
private static final String AUTH_TOKEN_TYPE_USERINFO_PROFILE =
"oauth2:https://www.googleapis.com/auth/userinfo.profile";
// TODO: allow the use to choose which account to use
Account acct = accountManager.getAccountsByType("com.google")[0];
accountManager.getAuthToken(acct, AUTH_TOKEN_TYPE_USERINFO_PROFILE,
null, this, new AccountManagerCallback() {
@Override
public void run(AccountManagerFuture future) {
try {
String accessToken = future.getResult().getString(
AccountManager.KEY_AUTHTOKEN);
Log.i(TAG, "Got OAuth2 access token: " + accessToken);
/*
Your code here. Use one of two options. In each case replace ... with
the above OAuth2 access token:
1) GET https://www.googleapis.com/oauth2/v1/userinfo?access_token=...
2) GET https://www.googleapis.com/oauth2/v1/userinfo with this header:
Authorization: Bearer ...
*/
} catch (OperationCanceledException e) {
// TODO handle this case
Log.w(TAG, "The user has did not allow access");
} catch (Exception e) {
// TODO handle this exception
Log.w(TAG, "Unexpected exception", e);
}
}
}, null);
}