Google日历API在公共日历中插入事件后返回错误401“需要登录" [英] Google calendar API returns error 401 'login required' on inserting event in public calendar

查看:82
本文介绍了Google日历API在公共日历中插入事件后返回错误401“需要登录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码在一个公共日历中插入一个事件

I want to insert an event in one of my public calendars using the following code

var mykey = '273183670752-8419q1upedso5lc0hvtgfnbv32rd67um.apps.googleusercontent.com';
var calendarid = '7a97f6k4ok8v77jc2po6kkgcuo%40group.calendar.google.com';  
var meeting = 'Lunchafspraak 2 juli om 13:00';
var urlContent = 'https://www.googleapis.com/calendar/v3/calendars/' + calendarid + '/events/quickAdd?text=' + meeting + '&key=' + mykey;

$.ajax({
    type: 'POST',
    url: urlContent,
    dataType: 'json',
    success: function (response) {
        alert("Event toegevoegd");
    },
    error: function (response) {
        alert(response.error.errors);
        alert("Event niet toegevoegd");
    }
});

但是它返回错误401'需要登录'

But it returns error 401 'login required'

我该怎么办,如何解决此问题?

What do I do wrong, and how do I solve this issue?

推荐答案

错误表示您正在使用的访问令牌已过期或无效.如本论坛,则需要使用客户端ID和客户端密码获取刷新令牌,以进行经过身份验证和授权的API调用.生成刷新令牌时,请确保使用正确的电子邮件登录.在这种情况下,它将是您自己的用户电子邮件地址,而不是服务帐户的电子邮件地址.服务帐户电子邮件地址用于服务器到服务器的应用程序.

Error 401 'login required' means that the access token you're using is either expired or invalid. As stated in this forum, you need to get the refresh token using the client ID and client secret to make authenticated and authorized API calls. Make sure you login with the right email when you are generating refresh token. In this case it would be your own user email address and not the service account email address. Service account email address is used for server to server applications.

检查这些链接:

  • Error with Google Calendar API - 401 Login required when adding a calendar event
  • Using API Key to authenticate a request generates a 401 error

希望这会有所帮助!

这篇关于Google日历API在公共日历中插入事件后返回错误401“需要登录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆