Google 数据(日历)API 请求的授权标题

发布于 2024-12-21 06:38:36 字数 2348 浏览 3 评论 0原文

我希望能够从 Google Calendar API 检索用户日历和事件。基本上,我设法从 Android 的 AccountManager 获取身份验证令牌并执行请求,但我得到了

  "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 错误,而不是列出我的日历。

如果你们有任何提示、文档等,我会很高兴。

编辑我只是尝试通过使令牌无效,

manager.invalidateAuthToken("com.google",authToken);

然后检索一个新的令牌,结果相同。

I want to be able to retrieve a users calendar and events from the Google Calendar API. Basically I managed to get an Auth-Token from Android's AccountManager and do the request, but I get

  "error": {
    "errors": [
    {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
    }
    ],
    "code": 401,
    "message": "Invalid Credentials"
    }
    }

As a response. The code producing it is (partial). When running this for the first time, Android asked me to authorize Access to my calendar data for my app, which I accepted.

    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

As you can see I am not using the google-api-java-client, because I could not get that one to fly either. Basically the example code calls methods that do not exist anymore / are moved to Android's AccountManager and the android-calendar-example produced a 401 eror instead of listing my calendars.

If you guys have any hints, documentation etc. I'd be glad.

edit I just tried invalidating the token via

manager.invalidateAuthToken("com.google",authToken);

then retrieving a new one via, with the same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2024-12-28 06:38:36

授权标头值黑白中有一个空格 - “GoogleLogin auth=xxxxxxx”。

There is a space in b/w the Authorization header value - "GoogleLogin auth=xxxxxxx".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文