Android Google Play服务会为blogger api返回无效令牌

时间:2014-11-28 17:21:27

标签: android google-play-services token blogger

我已获得访问博客API的权限,我已在我的开发者控制台上确认了这一点。 我还使用下面的一些代码编写了一些代码来使用Google Play服务执行oAuth2。

String SCOPE ="oauth2:https://www.googleapis.com/auth/blogger";
GoogleAuthUtil.getToken(context, "myEmail@gmail.com", mScope);

它返回一个令牌。正如它应该。 但是,一旦我尝试使用令牌访问api,我就会收到错误。

Unexpected response code 403 for https://www.googleapis.com/blogger/v3/users/self/blogs

这是我的要求: enter image description here

以下是我的回复:

enter image description here

以下是获取令牌的BaseActivity.java代码:

public class BaseActivity extends Activity {

static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1001;
static final int REQUEST_CODE_RECOVER_FROM_AUTH_ERROR = 1002;
private static final String SCOPE ="oauth2:https://www.googleapis.com/auth/blogger";
private String mEmail; // Received from newChooseAccountIntent(); passed to getToken()
public ProgressDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mDialog = new ProgressDialog(this);
    login();
}

public void login() {
    pickUserAccount();
}

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) {
        // Receiving a result from the AccountPicker
        if (resultCode == RESULT_OK) {
            mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);

            // With the account name acquired, go get the auth token
            getToken();
        } else if (resultCode == RESULT_CANCELED) {
            // The account picker dialog closed without selecting an account.
            // Notify users that they must pick an account to proceed.
            Toast.makeText(this, "Pick 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) {
        // Receiving a result that follows a GoogleAuthException, try auth again
        getToken();
    }
}

private void getToken() {
    if (mEmail == null) {
        pickUserAccount();
    } else {
        if (isDeviceOnline()) {
            new getTokenTask(BaseActivity.this, mEmail, SCOPE).execute();
        } else {
            Toast.makeText(this, "Not online", Toast.LENGTH_LONG).show();
        }
    }
}

/**
 * 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) {
    // Because this call comes from the AsyncTask, we must ensure that the following
    // code instead executes on the UI thread.
    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, BaseActivity.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 boolean isDeviceOnline() {
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    } else {
        return false;
    }
}

public class getTokenTask extends AsyncTask{
    Activity mActivity;
    String mScope;
    String mEmail;

    getTokenTask(Activity activity, String name, String scope) {
        this.mActivity = activity;
        this.mScope = scope;
        this.mEmail = name;
    }

    @Override
    protected Object doInBackground(Object[] params) {
        try {
            String token = fetchToken();
            Preferences.saveString(Constants.KEY_BLOGGER_TOKEN, token);
        } catch (IOException e) {
            // The fetchToken() method handles Google-specific exceptions,
            // so this indicates something went wrong at a higher level.
            // TIP: Check for network connectivity before starting the AsyncTask.
        }
        return null;
    }


    /**
     * Gets an authentication token from Google and handles any
     * GoogleAuthException that may occur.
     */
    protected String fetchToken() throws IOException {
        try {
            return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
        } catch (UserRecoverableAuthException userRecoverableException) {
            // GooglePlayServices.apk is either old, disabled, or not present
            // so we need to show the user some UI in the activity to recover.
            ((BaseActivity)mActivity).handleException(userRecoverableException);

        } catch (GoogleAuthException fatalException) {
            // Some other type of unrecoverable exception has occurred.
            // Report and log the error as appropriate for your app.
        }
        return null;
    }
}

}

我一直在撞墙挡住我的头。有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

终于明白了。

我的build.gradle文件不知何故最终拥有与我的清单不同的应用程序ID。我改变它,所以他们都匹配清单,繁荣!它奏效了。