我在我的Android应用程序中集成了Gmail API,并且我使用了API的入门页面提供的代码进行连接,我已经成功执行了该代码,现在我想从收件箱中获取消息,因为我使用了以下代码为此我得到了
403 forbidden error with message Insufficient permission
我没有弄错我在做什么?请帮帮我。
private class GetMessages extends AsyncTask<Void, Void, List<Message>> {
private com.google.api.services.gmail.Gmail mService = null;
private Exception mLastError = null;
GetMessages(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.gmail.Gmail.Builder(
transport, jsonFactory, credential)
.setApplicationName("Gmail API Android Quickstart")
.build();
}
/**
* Background task to call Gmail API.
* @param params no parameters needed for this task.
*/
@Override
protected List<Message> doInBackground(Void... params) {
try {
return getDataFromApi();
} catch (Exception e) {
mLastError = e;
cancel(true);
return null;
}
}
/**
* Fetch a list of Gmail labels attached to the specified account.
* @return List of Strings labels.
* @throws IOException
*/
private List<Message> getDataFromApi() throws IOException {
// Get the labels in the user's account.
String user = "me";
List<Message> messages = new LinkedList<>();
ListMessagesResponse listMessageResponse =null;
Gmail.Users.Messages.List request = mService.users().messages().list(user)
.setLabelIds(Arrays.asList("INBOX"))
.setMaxResults(Long.parseLong("10"))
;
do {
listMessageResponse = request.execute();
messages.addAll(listMessageResponse.getMessages());
request.setPageToken(listMessageResponse.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
return messages;
}
@Override
protected void onPreExecute() {
mOutputText.setText("");
mProgress.show();
}
@Override
protected void onPostExecute(List<Message> output) {
mProgress.hide();
if (output == null || output.size() == 0) {
mOutputText.setText("No results returned.");
} else {
mOutputText.setText(TextUtils.join("\n", output));
}
}
@Override
protected void onCancelled() {
mProgress.hide();
if (mLastError != null) {
if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
showGooglePlayServicesAvailabilityErrorDialog(
((GooglePlayServicesAvailabilityIOException) mLastError)
.getConnectionStatusCode());
} else if (mLastError instanceof UserRecoverableAuthIOException) {
startActivityForResult(
((UserRecoverableAuthIOException) mLastError).getIntent(),
MainActivity.REQUEST_AUTHORIZATION);
} else {
mOutputText.setText("The following error occurred:\n"
+ mLastError.getMessage());
}
} else {
mOutputText.setText("Request cancelled.");
}
}
}