Google Calendar API 401错误表示身份验证失败,服务器拒绝了请求,因为提供的凭据无效或缺失。这是HTTP状态码中的"Unauthorized"错误。
const {google} = require('googleapis');
// 确保scope包含日历访问权限
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
// 创建OAuth2客户端
const oAuth2Client = new google.auth.OAuth2(
'YOUR_CLIENT_ID',
'YOUR_CLIENT_SECRET',
'YOUR_REDIRECT_URL'
);
// 获取授权URL
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('授权URL:', authUrl);
from google.oauth2 import service_account
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = 'service-account.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# 确保已启用域范围授权(如适用)
delegated_credentials = credentials.with_subject('user@domain.com')
service = build('calendar', 'v3', credentials=delegated_credentials)
确保在Google Cloud控制台中:
使用OAuth 2.0 Playground验证令牌:
https://developers.google.com/oauthplayground/
Google Calendar API常用于:
通过系统性地检查这些方面,通常可以解决401无效凭据错误。
没有搜到相关的文章