繁体   English   中英

Android Google Play服务为博客API返回无效令牌

[英]Android Google Play Services returns invalid token for blogger api

我已获得访问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

这是我的要求: 在此处输入图片说明

这是我的回应:

在此处输入图片说明

这是获取令牌的我的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;
    }
}

}

我一直在把头撞在墙上。 有人有想法么?

终于想通了。

我的build.gradle文件最终以不同于清单的方式获得了不同的应用程序ID。 我对其进行了更改,使它们都与清单相符,并且繁荣! 有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM