我正在尝试对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,希望有人可以提出任何想法。谢谢
答案 0 :(得分:0)
启用Google Task API。它在你的API& AUTH