Credentials Error When Integrating Google Drive With

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 Drive API Service Account Authentication fails

I have solved the problem..... I was wrong the authentication token

code: 401, message: Invalid Credentials -Google drive connection error

problem was been in the code -> wrong way to grab the refresh token, the correct code is:

 } else {

try{
$token = json_decode($accessToken, true);
$this->client->setAccessToken($token);


if ($this->client->isAccessTokenExpired()) {
if ($this->client->getRefreshToken()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
}

}

}catch(Exception $e){

$accessToken = null;
$auth_url = $this->client->createAuthUrl();


}

Now app works perfetly, nomore disconnection!

Authentication problem with google drive API in python

I found the answer here
You need to add these 2 SCOPES together:

SCOPES = ['https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.file']

Google Drive API - Invalid service account credentials

Invalid service account credentials

Normally means that the key fine you are using is invalid. You need to create a service account key file.

If you open the json key file it should look something like this

{
"type": "service_account",
"project_id": "Redacted-305109",
"private_key_id": "Redacted",
"private_key": "-----BEGIN PRIVATE KEY-----Redacted---END PRIVATE KEY-----\n",
"client_email": "Redacted@testapikey-305109.iam.gserviceaccount.com",
"client_id": "Redacted",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/Redacted-305109.iam.gserviceaccount.com"
}

how to create a service account key file

Docs: google-api-nodejs-client#service-account-credentials

const {google} = require('googleapis');

const auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your-secret-key.json',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});


Related Topics



Leave a reply



Submit