You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Calendar API Key测试返回401错误:API密钥无效求助

Google Calendar API密钥在Excel VBA中返回401无效凭证错误

问题背景

  • 已正常使用Google Maps API密钥数月,现需通过Excel VBA代码创建日历事件
  • 在Google Cloud Console的项目中启用了Calendar API,并专门为此申请了新API密钥,尝试填写OAuth2相关信息但对OAuth2完全不了解
  • 首次尝试用代码创建事件失败后,使用Gemini提供的测试代码运行,收到“API Key Invalid - Status Code 401”错误
  • 对比Calendar API密钥设置与正常工作的Distance Matrix、Geocoding API密钥设置,未发现差异;确认账号无未支付账单
  • 项目归属专门创建的Google账号,计划将目标日历共享给2-3人查看事件

测试VBA代码

Sub TestGoogleCalendarAPIKey()

  ' Replace with your actual API key
  Const apiKey As String = "YOUR_API_KEY"

  ' Base URL for the Google Calendar API
  Const baseUrl As String = "https://www.googleapis.com/calendar/v3/calendars/"

  ' Set up the request
  Dim xmlHttp As Object
  Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
  xmlHttp.Open "GET", baseUrl & "primary", False
  xmlHttp.setRequestHeader "Authorization", "Bearer " & apiKey

  ' Send the request
  On Error Resume Next
  xmlHttp.send
  On Error GoTo 0

  ' Check for success
  If xmlHttp.Status = 200 Then
    ' API key is valid
    MsgBox "API key is valid.", vbInformation
  Else
    ' API key is invalid
    MsgBox "API key is invalid. Status code: " & xmlHttp.Status, vbCritical
  End If

  Set xmlHttp = Nothing

End Sub

注:已将代码中的YOUR_API_KEY替换为实际申请的Calendar API密钥后运行

错误响应内容

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

内容的提问来源于stack exchange,提问作者Gavin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:16:08