我正在尝试使用 Android 应用程序访问 Gmail API。我已使用所有可用范围组合成功请求并接收到访问令牌。但似乎每次我实际尝试使用 Gmail API 命令时,我都会得到一个403 exception reading: Access not configured please use Google developers console to activate the api...
不用说 API 已激活,并使用正确的包名称和 sha1 代码创建了客户端密钥。相同的客户端 ID 可以很好地与 Google Plus 功能配合使用。
有人遇到此问题或成功从 Android 连接到 Gmail api 吗?
谢谢
这是日志:
09-04 21:40:54.014: W/System.err(26717): com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
09-04 21:40:54.014: W/System.err(26717): {
09-04 21:40:54.014: W/System.err(26717): "code" : 403,
09-04 21:40:54.014: W/System.err(26717): "errors" : [ {
09-04 21:40:54.014: W/System.err(26717): "domain" : "usageLimits",
09-04 21:40:54.014: W/System.err(26717): "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project.",
09-04 21:40:54.014: W/System.err(26717): "reason" : "accessNotConfigured"
09-04 21:40:54.014: W/System.err(26717): } ],
09-04 21:40:54.014: W/System.err(26717): "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project."
09-04 21:40:54.014: W/System.err(26717): }
除了 Google+ API 和 Gmail API 之外,是否还有其他 API 需要在 API 控制台中启用?
编辑:
我发现使用基于 WebView 的身份验证会产生一个访问令牌,该令牌将授予 Gmail API 访问权限,但这不是一个有效的解决方案,因为该令牌是短暂的。至于现在 GoogleAuthUtil 将授予令牌,但它的权限不足以使用 Gmail API。有人成功地将 Android 与 Gmail API 连接起来并愿意分享吗?
编辑2:
以下是我正在做的事情的片段:
获取令牌:
token = GoogleAuthUtil.getToken(MainActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient), scope);
尝试从 Gmail API 获取邮件:
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();
service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName("GmailApiTP").build();
ListMessagesResponse messagesRespose;
List<Message> m = null;
ArrayList<String> ids = new ArrayList<String>();
ids.add("INBOX");
try {
messagesRespose = service.users().messages().list("me").setLabelIds(ids).setQ("From: something")
.execute();
m = messagesRespose.getMessages();
} catch (IOException e) {
e.printStackTrace();
}
使用 Gmail API 服务时会捕获异常。此外,我尝试清除令牌并要求一个具有相同结果的新令牌。