2013-02-26 10 views

答えて

3

API referenceをご覧になりましたか?

あなたが探しているクラスは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(); 
} 
+5

スコープとは何ですか? – Sameer

+1

この[link](https://gist.github.com/ianbarber/9607551)では、正確にトークンを取得する方法について説明しています。これらのコードを非同期タスクにラップします。 – Arefin

+0

@arefinはアカウント名です。指定されたリンクのメールIDを指しますか? –

3

のOAuthスコープ(ただのGooglerのために有用であることが)疑問に答えるために:完全に理解するために

を、グーグル、それは認証と認可の概念についていくつか。

ユーザー/パスワードが存在するかどうかは、部分であることを確認してください。

範囲は承認部分に必要となります:あなたがユーザーに代わって行う、または受け取ることが許可されているもの。許可されるスコープのリストを取得するには、OAuthサービスのドキュメントを確認してください。

「OpenIDのプロフィールメールhttps://www.googleapis.com/auth/plus.loginhttps://www.googleapis.com/auth/plus.me」:https://developers.google.com/+/api/oauth?hl=pt-ZA

例えば、利用者からの可能なすべての情報を取得するには、スコープを使用することができます。最も一般的なスコープが上で見つけることができ

GoogleやG +から

(最初のワードは、プロトコルを参照して、応答のフィールドを求める単語、及び所望のスコープに続くスペースセパレータtoghether宣言することができる)

注:アクセストークンを使用してスコープで以前に要求しなかったことを要求または実行しようとすると、サービスは認証エラーを返す可能性があります。 Googleにとって

、あなたが彼のOAuthサービスと範囲について学ぶために使用することができます良いツールがOAuthの遊び場です:https://developers.google.com/oauthplayground/

0

あなたは非同期タスクを使用して、それを取得する必要があります。

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; 
} 

あなたのアクセストークンはトークン変数に格納されます。

関連する問題