Google API - Invalid Credentials

Google Drive API - Invalid Credentials

The result of the link below is an Authorization code.

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=735795831119-kcpkamhiaojavqrt67mti7thcaa6ce87.apps.googleusercontent.com

You need to exchange it to https://accounts.google.com/o/oauth2/token to generate an Access Token:

curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token

The result of the curl above is something like this:

{
"access_token": "access token here",
"expires_in": 3599,
"refresh_token": "refresh token here",
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}

Now you have the access token, you can paste it in the code below alongside with your API key.

curl \
'https://www.googleapis.com/drive/v3/files?corpora=user&q=createdTime%20%3E%20%272021-11-23T12%3A00%3A00%27&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed

Note:

  • Make sure you enable the Drive API in GCP
  • Application Client Id and Application Client Secret can be found after you created an OAuth 2.0 Client ID in GCP.

Reference:

  • DaImTo answer on How to connect to the Google Drive API using cURL.

Google API - Invalid Credentials

Try adding the access token to the headers of your Alamofire request instead of passing it as a parameter. As such:

let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"]
let request = Alamofire.request(path, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)


Related Topics



Leave a reply



Submit