1

AndroidアプリケーションとWebサーバーが連携しています。ここで、Android アプリケーションから Google 経由でユーザーにログインしてもらいます (または、Android で Google アカウントのいずれかを使用します)。次に、andriod アプリケーションは、サービス呼び出しでトークンを Web サーバーに渡します...そして、そのトークンを持つ Google からユーザーの電子メールまたはプロファイル データを取得する方法を理解できません。

ブラウザで次のような呼び出しを行うことができます: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= {accessToken}

しかし、Googleライブラリを使用してそれを行う方法は? 使用するライブラリなどは?

4

3 に答える 3

1

ここからコード サンプルを使用しました: https://github.com/googleplus/gplus-verifytoken-javaは最新のようです。コードは次のようになりました。

GoogleCredential credential = new GoogleCredential().setAccessToken( accessToken );
Oauth2 oauth2 = new Oauth2.Builder(
    UrlFetchTransport.getDefaultInstance(),
    JacksonFactory.getDefaultInstance(), credential )
    .build();
Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken( accessToken ).execute();

// ... check tokeninfo expiry and issued to etc ...
于 2014-10-07T12:06:00.663 に答える
1

使用しようとしているサービスに応じて、適切なGoogle のクライアント ライブラリを選択し、 Google+ サンプルを確認してください。

前半は基本的にすべての API で同じはずです。oauth2ユーザー情報を取得するには、ライブラリが必要で、次のようなことを行います (この例から抜粋):

// Set up the HTTP transport and JSON factory
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

// Set up OAuth 2.0 access of protected resources
// using the refresh and access tokens, automatically
// refreshing the access token when it expires
GoogleAccessProtectedResource requestInitializer =
    new GoogleAccessProtectedResource(accessToken, httpTransport,
    jsonFactory, clientId, clientSecret, refreshToken);

// set up global Oauth2 instance
Oauth2 oauth2 = new Oauth2.Builder(httpTransport, jsonFactory, requestInitializer)
    .setApplicationName("Google-OAuth2Sample/1.0").build();

Userinfo userinfo = oauth2.userinfo().get().execute();
于 2012-07-04T14:53:18.307 に答える