admin管理员组

文章数量:1128733

I have been using Google maps API keys successfully for a few months, now I need to create Calendar Events from my Excel VBA code. So I enabled the Calendar API in my Cloud Console project and requested A new API key specifically for this API alone. I also filled the OAuth2 info as best I could as I have no idea what that is about. Then I tried the API key in my code but it failed to create the new Event, so asked Google Gemini for sample code to test my API. I ran that code and received this response: "API Key Invalid - Status Code 401."

I have revisited the Cloud Console and compared the Calendar API key set-up to my Distance Matrix and Geocoding API keys which work perfectly and the set-up appears identical. I have checked there is no outstanding payment in the Billing section. I don't know what else to check or do to get this working. To be honest I find the whole Cloud Console rather overwhelming and I don't really know what's going on there. I had to ask Gemini for specific instructions on how to do anything and everything, but even Gemini can't help resolve this issue.

I have absolutely no idea what is going on with OAuth2 so perhaps that is my problem? But then I would expect Gemini to know about that and to have guided me accordingly.

The project in the Google Cloud Console is listed under a Google Account that I created specifically for this purpose and my plan is to share this Calendar with 2 or 3 other people so we can all see the upcoming calendar events.

If anyone can help guide me through getting the key to work I would most appreciative.

After the initial failure to create a Calendar Event from my Excel VBA function, I asked Gemini for a testing function and was given this...

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 = "/"

  ' 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

Obviously I replaced YOUR_API_KEY with my new API key as instructed and ran the sub to receive the error described above.

As per Tim's comment I now include the xmlHttp.responseText below. Thanks Tim.

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See .",
    "errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

本文标签: excelGoogle Calendar API Key test returns quotAPI Key InvalidStatus Code 401quotStack Overflow