【发布时间】:2014-10-29 08:39:43
【问题描述】:
我正在尝试使用 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 连接起来并愿意分享吗?
EDIT2:
这是我正在做的事情的 sn-ps:
获取令牌:
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 服务时捕获异常。此外,我尝试清除令牌并要求一个具有相同结果的新令牌。
【问题讨论】:
-
你尝试实现这个例子吗(developers.google.com/gmail/api/quickstart/…)?
-
是的。这很简单。但仍然没有解释为什么 GooglaApiCient 返回一个不可用的令牌。
-
就像一个数据点一样,
GoogleAuthUtil在我使用 Gmail API 从 Jelly Bean 和 KitKat 上的服务发送电子邮件时效果很好。我直接使用 REST API(即通过HttpURLConnection)而不是 Java 客户端库,但这应该与令牌获取无关。在我正确设置 Android 应用程序的客户端 ID 之前,我收到了您的 403 一段时间,但之后自动处理刷新就很好了。您未显示的一个可能出错的地方是在getToken()呼叫中处理UserRecoverableAuthExtension。 -
设置客户端 ID 的任何特殊方式。我已经做了很多次,但仍然遇到相同的 403 错误。 UserRecoverableAuthExtension 已处理。
-
this page 的第 2 步描述了对我有用的方法。您也可以尝试在您的开发机器上使用令牌,例如卷曲:
curl -v -H "Authorization: Bearer ya29.pwAy..." https://www.googleapis.com/gmail/v1/users/me/messages。这将在生成令牌后工作一个小时。如果您想让我看到,请在任何回复中添加@rhashimoto。
标签: android google-oauth gmail-api google-schemas google-oauth-java-client