调用Google Calendar API时遇401无效凭证错误求助
Hey there, let's walk through fixing this 401 error—this is one of the most common snags with Google's OAuth 2.0 flow, so we'll get your Calendar access working in no time.
First, let's recap the error you're hitting:
Type: Google_Service_Exception
Message: Error calling GET https://www.googleapis.com/oauth2/v2/userinfo: (401) Invalid Credentials
Triggered in/opt/lampp/htdocs/CI/application/libraries/Googleplus.phpline 62 when callinggetUser()
Here are the key steps to diagnose and fix this:
Verify you're using the right credential type
In the Google Cloud Console, you need an OAuth 2.0 Client ID (not a Service Account Key) for user-facing Calendar access. Double-check that you created the correct credential type, and selected the right application type (Web app, since you're using CodeIgniter).Check your OAuth scopes
Make sure your authorization flow requests the necessary scopes. For accessing user info and Calendar, you need at least:https://www.googleapis.com/auth/userinfo.profile(for the userinfo endpoint)https://www.googleapis.com/auth/calendar(for Calendar access)
Confirm these scopes are listed in your code's authorization request, and that they're enabled in the Google Cloud Console's OAuth consent screen.
Handle access token expiration
Google's access tokens expire after 1 hour. If your code isn't refreshing tokens automatically, you'll hit this 401 once the token is stale. Check yourGoogleplus.phporGooglecalendar.phpcode for logic like this:$client->setAccessToken($stored_access_token); if ($client->isAccessTokenExpired()) { // Refresh the token using the stored refresh token $new_token = $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); // Save the new access token (and refresh token if updated) to your session/database $client->setAccessToken($new_token); }If this logic is missing or broken, your token will expire and trigger the error.
Confirm redirect URIs match exactly
In the Google Cloud Console, the authorized redirect URI for your OAuth client must match the one in your CodeIgniter app exactly—includinghttp/https, port number, and full path. For example, if your callback ishttp://localhost/CI/main/oauth-callback, the console's URI must be identical (no trailing slashes, no typos).Validate your access token
Test your stored access token outside your app to rule out code issues. Use curl or Postman to send a GET request tohttps://www.googleapis.com/oauth2/v2/userinfowith this header:Authorization: Bearer YOUR_ACCESS_TOKENIf this returns a 401, the token itself is invalid—meaning your authorization flow isn't generating a valid token. If it returns user data, the problem is in how your code is passing the token to the Google client.
Check API enablement
Make sure both the Google Calendar API and Google OAuth2 API are enabled in the Google Cloud Console's "APIs & Services" > "Enabled APIs & Services" section. Even if you have the right credentials, disabled APIs will throw authorization errors.Double-check credential configuration
Ensure your client ID, client secret, and redirect URI are correctly copied into your CodeIgniter config orGoogleplus.phpfile. A single typo (like a missing character in the client secret) will break the entire flow.
Start with these checks—most 401 Invalid Credentials issues boil down to one of these points. Let me know if you hit a snag in any step!
内容的提问来源于stack exchange,提问作者dev_02




