I am new to clasp.
After the initial login via: clasp login I am able to login to script.google.com Next, I have created a project and pushed the file via: clasp push
Now, I have logged out using: clasp logout
Help required here: Now, if I am trying:
clasp login --creds ./.clasp.json
I am getting, "Error retrieving access token: TypeError: Cannot read property 'project_id' of undefined".
Please guide me on how to login via --creds?
TLDR: You're using the config file (.clasp.json
), and not a credentials file (creds.json
or other) from the Google Cloud Project console.
When you log in, the default storage of credentials is in a file named .clasprc.json
in the ~
directory (C:\Users\<user>\
on Windows):
$ clasp login
Logging in globally...
š Authorize clasp by visiting this url:
https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&.....
Authorization successful.
Default credentials saved to ~\.clasprc.json (C:\Users\<user>\.clasprc.json).
Note that this file (.clasprc.json
) is not the same as .clasp.json
.
The contents of this file purportedly depend on the auth type, global or local:
// GLOBAL: clasp login will store this (~/.clasprc.json):
{
"access_token": "XXX",
"refresh_token": "1/k4rt_hgxbeGdaRag2TSVgnXgUrWcXwerPpvlzGG1peHVfzI58EZH0P25c7ykiRYd",
"scope": "https://www.googleapis.com/auth/script.projects https://www.googleapis.com/auth/script ...",
"token_type": "Bearer",
"expiry_date": 1539130731398
}
Local auth stores the client secret / etc, and is generally required if you plan to use clasp run
to execute a function via the Google Apps Script API.
// LOCAL: clasp login will store this (./.clasprc.json):
{
"token": {
// as above
},
// Settings
"oauth2ClientSettings": {
"clientId": "807925367021-infvb16rd7lasqi22q2npeahkeodfrq5.apps.googleusercontent.com",
"clientSecret": "9dbdeOCRHUyriewCoDrLHtPg",
"redirectUri": "http://localhost"
},
"isLocalCreds": true
}
(In practice, both files will have the format of the LOCAL
file--properties token
, oauth2ClientSettings
, and isLocalCreds
--though the value of isLocalCreds
will be false for a global login.)
{
"scriptId": "",
"rootDir": "build/",
"projectId": "project-id-xxxxxxxxxxxxxxxxxxx",
"fileExtension": "ts",
"filePushOrder": ["file1.ts", "file2.ts"]
}
Note that clasp.json
is configuration of the script files and clasprc.json
is stored credential / authorization of the user. Neither of them is the appropriate credential file for logging in locally.
The specific error you get is a result of you providing the incorrect file. Your supplied "credential" file does not have the required properties, and thus when clasp attempts to read from that property
console.log(LOG.CREDS_FROM_PROJECT(options.creds.installed.project_id));
you get the error:
Error retrieving access token: TypeError: Cannot read property 'project_id' of undefined
You can obtain the proper credentials file from your Apps Script project's Google Cloud Project page, i.e. https://console.cloud.google.com/apis/credentials?authuser=0&project=<some project id>
This file will have the format:
{
"installed":{
"client_id":"<stuff>.apps.googleusercontent.com",
"project_id":"<some project id>",
"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_secret":"<more stuff>",
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
}
}
If your credentials file does not have that format, you cannot use it to log in locally.
I also had this error, when I incorrectly chose "Web Application" for oauth type, instead of "Desktop Application", in this case main section is "web", instead of "installed", and I receive same
Error retrieving access token: TypeError: Cannot read property 'project_id' of undefined
Wrong oauth client looked like
{
"web": {
"client_id": "<STUFF>",
"project_id": "<STUFF>",
"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_secret": "<STUFF>"
}
}
while correct one was
{
"installed": {
"client_id": "<STUFF>",
"project_id": "<STUFF>",
"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_secret": "<STUFF>",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
The script tries to resolve "installed.project_id" variable and fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With