Play サービス 8.1 の Google+ サインインから Play サービス 8.3 の新しい Google サインインに切り替えようとすると、サーバー側のコンポーネントが動作を停止し、Google サーバーへのすべての API 呼び出しで 401 をスローし始めます。
Android アプリが Google Play Services 8.1 を使用していた場合:
build.gradle:
compile 'com.google.android.gms:play-services-plus:8.1.0'
Android クライアント:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addScope(new Scope("email"))
.build();
String scopes = "oauth2:profile email";
accessToken = GoogleAuthUtil.getToken(
SignInActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
scopes); // then send accessToken to the server
サーバ側:
// get user details from Google+ REST API
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Plus plusService = new Plus.Builder(new NetHttpTransport(), new JacksonFactory(), null)
.setApplicationName("AppName")
.setHttpRequestInitializer(credential)
.build();
Person person = plusService.people().get("me").execute();
Google Play サービス 8.3 に切り替える場合:
build.gradle:
compile 'com.google.android.gms:play-services-auth:8.3.0'
Android アプリ:
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestServerAuthCode("GOOGLE_CLIENT_ID", false)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build();
同じサーバー コードで、get("me") 呼び出しは次を返します。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}