【问题标题】:Authorization-Heades for Google Data (Calendar) API requestGoogle 数据(日历)API 请求的授权标头
【发布时间】: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


    【解决方案1】:

    Authorization 标头值中有一个空格 - "GoogleLogin auth=xxxxxxx".

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 1970-01-01
      • 2017-09-24
      • 2017-01-27
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      相关资源
      最近更新 更多