【发布时间】:2011-12-14 09:57:33
【问题描述】:
我希望能够从 Google Calendar API 检索用户日历和事件。基本上我设法从 Android 的 AccountManager 获取了一个 Auth-Token 并执行了请求,但我得到了
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
作为回应。产生它的代码是(部分)。第一次运行时,Android 要求我授权访问我的应用程序的日历数据,我接受了。
final Account account = manager.getAccountsByType("com.google")[0];
AccountManagerFuture<Bundle> accountManagerFuture =
manager.getAuthToken(account, "cl", true,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
final String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
final Uri usersCalendars = Uri.parse(
"https://www.googleapis.com/calendar/v3/users/me/calendarList")
.buildUpon()
.appendQueryParameter("key","--myapikey--")
.build();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(usersCalendars.toString());
request.setHeader("Authorization","GoogleLoginAuth=" + authToken);
request.setHeader("GData-Version","3.0");
HttpResponse response = client.execute(request);
// outputting response, logging etc
如您所见,我没有使用 google-api-java-client,因为我也无法让那个客户端飞行。基本上,示例代码调用不再存在的方法/被移动到 Android 的 AccountManager 并且 android-calendar-example 产生了 401 错误,而不是列出我的日历。
如果你们有任何提示、文档等,我会很高兴。
edit 我刚刚尝试通过
使令牌无效manager.invalidateAuthToken("com.google",authToken);
然后通过检索一个新的,结果相同。
【问题讨论】:
标签: android authentication google-calendar-api google-data-api