2014-12-25 199 views
1

我使用必須連接到Google的應用程序,獲取accessToken並將其發送到服務器端。在服務器端,此令牌用於連接到Gmail帳戶並獲取所有聯繫人。 這是我的和平碼,我如何獲得訪問令牌:服務器端的Google訪問令牌

private static final String SCOPE = "oauth2: https://www.googleapis.com/auth/userinfo.profile" + 
     "       https://www.googleapis.com/auth/userinfo.email" + 
     "       https://www.googleapis.com/auth/plus.login"; 
static final int REQUEST_CODE_PICK_ACCOUNT = 1000; 
static final int REQUEST_CODE_RECOVER_FROM_AUTH_ERROR = 1001; 
static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1002; 
String mEmail; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_social_auth); 

    pickUserAccount(); 

} 

/** Starts an activity in Google Play Services so the user can pick an account */ 
private void pickUserAccount() { 
    String[] accountTypes = new String[]{"com.google"}; 
    Intent intent = AccountPicker.newChooseAccountIntent(null, null, 
      accountTypes, false, null, null, null, null); 
    startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_CODE_PICK_ACCOUNT) { 
     if (resultCode == RESULT_OK) { 
      mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 
      new AccessToken(this, mEmail, SCOPE).execute(); 
     } else if (resultCode == RESULT_CANCELED) { 
      Toast.makeText(this, "You must pick an account", Toast.LENGTH_SHORT).show(); 
     } 
    } else if ((requestCode == REQUEST_CODE_RECOVER_FROM_AUTH_ERROR || 
      requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR) 
      && resultCode == RESULT_OK) { 
     handleAuthorizeResult(resultCode, data); 
     return; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

private void handleAuthorizeResult(int resultCode, Intent data) { 
    if (data == null) { 
     show("Unknown error, click the button again"); 
     return; 
    } 
    if (resultCode == RESULT_OK) { 
     Log.i(TAG, "Retrying"); 
     mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 
     new AccessToken(this, mEmail, SCOPE).execute(); 
     return; 
    } 
    if (resultCode == RESULT_CANCELED) { 
     show("User rejected authorization."); 
     return; 
    } 
    show("Unknown error, click the button again"); 
} 

/** 
* This method is a hook for background threads and async tasks that need to provide the 
* user a response UI when an exception occurs. 
*/ 
public void handleException(final Exception e) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (e instanceof GooglePlayServicesAvailabilityException) { 
       // The Google Play services APK is old, disabled, or not present. 
       // Show a dialog created by Google Play services that allows 
       // the user to update the APK 
       int statusCode = ((GooglePlayServicesAvailabilityException)e) 
         .getConnectionStatusCode(); 
       Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode, 
         GoogleAuthActivity.this, 
         REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR); 
       dialog.show(); 
      } else if (e instanceof UserRecoverableAuthException) { 
       // Unable to authenticate, such as when the user has not yet granted 
       // the app access to the account, but the user can fix this. 
       // Forward the user to an activity in Google Play services. 
       Intent intent = ((UserRecoverableAuthException)e).getIntent(); 
       startActivityForResult(intent, 
         REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR); 
      } 
     } 
    }); 
} 


public void show(final String message) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
    }); 
} 


class AccessToken extends AsyncTask<Void, Void, String> { 

    GoogleAuthActivity context; 
    String email; 
    String scope; 

    AccessToken(GoogleAuthActivity context, String email, String scope) { 
     this.context = context; 
     this.email = email; 
     this.scope = scope; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     String token = null; 
     try { 
      token = GoogleAuthUtil.getToken(context, email, scope); 
     } catch (UserRecoverableAuthException userRecoverableException) { 
      // GooglePlayServices.apk is either old, disabled, or not present, which is 
      // recoverable, so we need to show the user some UI through the activity. 
      // короче жопа((
      context.handleException(userRecoverableException); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (GoogleAuthException e) { 
      e.printStackTrace(); 
     } 
     return token; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     sendToServer(s); 
    } 
} 

服務器開發人員使用這個網址來獲得訪問:https://www.googleapis.com/plus/v1/people/[id]/people/visible?access_token=[myAccessToken] 但在服務器端,我得到403錯誤:訪問未配置。 API未針對您的項目啓用,或者API密鑰上配置了per-IP或per-Referer限制,並且請求不符合這些限制。請使用Google Developers Console更新您的配置。

如果我只用https://www.googleapis.com/auth/userinfo.profile的範圍內,我們得到一個錯誤:「權限不足」

在開發者控制檯啓用聯繫人API和Google+ API。

我想從android設計中獲取有用的訪問令牌,將其發送到服務器端,並從中獲取用戶聯繫人。我做錯了什麼?

回答

相關問題