こことこれで最後のGoogle +ニュースを読んだ後。サインインを完了した後、アクセス トークンを取得するにはどうすればよいですか?
3 に答える
oauth スコープに関する疑問に答えるには (Google 社員に役立つように):
完全に理解するには、認証と承認の概念について Google で調べてください。
ユーザー/パスワードが存在するかどうかを確認するのは、認証部分に関するものです。
スコープは承認部分に必要です: ユーザーに代わって何をするか、何を受け取ることが承認されているか。許可されているスコープのリストを取得するには、OAuth サービスのドキュメントを確認してください。
Google および G+ の最も一般的なスコープは、https ://developers.google.com/+/api/oauth?hl=pt-ZA にあります。
たとえば、ユーザーから可能なすべての情報を取得するには、スコープを使用できます。
"openid プロファイル メールhttps://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me "
(最初の単語はプロトコルを参照し、その後に応答のフィールドを要求する単語が続きます。目的のスコープは、スペース区切り文字と共に宣言できます)
注: 後で、アクセス トークンを使用して、スコープを使用して以前に要求していないことを要求または実行しようとすると、サービスは承認エラーを返す可能性があります。
Google の場合、彼の OAuth サービスとスコープについて学ぶために使用できる優れたツールは、OAuth Playground: https://developers.google.com/oauthplayground/です。
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();
}
非同期タスクを使用してフェッチする必要があります。
public void onConnected(Bundle connectionHint) {
// Reaching onConnected means we consider the user signed in.
Log.i(TAG, "onConnected");
// Update the user interface to reflect that the user is signed in.
mSignInButton.setEnabled(false);
mSignOutButton.setEnabled(true);
mRevokeButton.setEnabled(true);
// Retrieve some profile information to personalize our app for the user.
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
AsyncTask<Void, Void, String > task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String token = null;
final String SCOPES = "https://www.googleapis.com/auth/plus.login ";
try {
token = GoogleAuthUtil.getToken(
getApplicationContext(),
Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + SCOPES);
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
}
return token;
}
@Override
protected void onPostExecute(String token) {
Log.i(TAG, "Access token retrieved:" + token);
}
};
task.execute();
System.out.print("email" + email);
mStatus.setText(String.format(
getResources().getString(R.string.signed_in_as),
currentUser.getDisplayName()));
Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
.setResultCallback(this);
// Indicate that the sign in process is complete.
mSignInProgress = STATE_DEFAULT;
}
アクセス トークンは token 変数に格納されます。