簡體   English   中英

403 Google API上的錯誤

[英]403 Error on Google Task API

我正在嘗試對Google Tasks API進行REST調用。但它總是返回以下內容

 {
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. The API (Tasks API) is not enabled for your project. Please use the Google Developers Console to update your configuration.",
    "extendedHelp": "https://console.developers.google.com"
   }
  ],
  "code": 403,
  "message": "Access Not Configured. The API (Tasks API) is not enabled for your project. Please use the Google Developers Console to update your configuration."
 }
}

這是我的編碼,范圍是oauth2: https//www.googleapis.com/auth/tasks

  private void getTasksList() throws IOException, JSONException {
        String token = fetchToken();
        System.out.println("=========="+token);

        if (token == null) {
          // error has already been handled in fetchToken()
          return;
        }
        URL url = new URL("https://www.googleapis.com/tasks/v1/users/@me/lists?access_token="+token );
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        int sc = con.getResponseCode();
        System.out.println("Return Code:"+sc);
        if (sc == 200) {
          InputStream is = con.getInputStream();
          String name = getFirstName(readResponse(is));
          is.close();
          return;
        } else if (sc == 401) {
            GoogleAuthUtil.invalidateToken(mActivity, token);
            onError("Server auth error, please try again.", null);
            Log.i(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
            return;
        } else {
          onError("Server returned the following error code: " + sc, null);
            Log.i(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
          return;
        }
    }
protected String fetchToken() throws IOException {
        try {
            return GoogleAuthUtil.getToken(mActivity, mEmail, StaticValue.SCOPE2);
        } 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.
            mActivity.handleException(userRecoverableException);
        } catch (GoogleAuthException fatalException) {
            onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
        }
        return null;
    }

第二部分,GoogleSync是一個AsyncTask,並在doInBackground中調用getTaskList()

 private void getTasksListFromGoogle() {
        if (mEmail == null) {
            pickUserAccount();
        } else {
            if (Utility.IsNetworkEnable(this)) {
                new GoogleSync(this, mEmail).execute();
            } else {
                Toast.makeText(this, R.string.noNetwork, Toast.LENGTH_LONG).show();
            }
        }
    }

    private void pickUserAccount() {
        String[] accountTypes = new String[]{StaticValue.COM_GOOGLE};
        Intent intent = AccountPicker.newChooseAccountIntent(null, null,accountTypes, false, null, null, null, null);
        startActivityForResult(intent, StaticValue.REQUEST_CODE_PICK_ACCOUNT);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == StaticValue.REQUEST_CODE_PICK_ACCOUNT) {
            if (resultCode == RESULT_OK) {
                mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                getTasksListFromGoogle();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, R.string.pick_account, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void handleException(final Exception e) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (e instanceof GooglePlayServicesAvailabilityException) {
                    int statusCode = ((GooglePlayServicesAvailabilityException)e).getConnectionStatusCode();
                    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode,MainActivity.this,StaticValue.REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
                    dialog.show();
                } else if (e instanceof UserRecoverableAuthException) {
                    Intent intent = ((UserRecoverableAuthException)e).getIntent();
                    startActivityForResult(intent,StaticValue.REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
                }
            }
        });
    }

這是我的API控制台,com.kmconcept.googletasklist是我的包名i.stack.imgur.com/89zG0.png

i.stack.imgur.com/paKgr.png

我猜oauth2有問題,關鍵是使用debugkey,希望有人可以提出任何想法。 謝謝

啟用Google Task API。 它在你的API和auth中

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM