Error retrieving access token: TypeError: Cannot read property 'project_id' of undefined

我的未来我决定 提交于 2020-06-16 03:42:08

问题


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?


回答1:


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.

clasprc.json format:

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.)

clasp.json format:

{
  "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.

Resolving the error

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.



来源:https://stackoverflow.com/questions/54533397/error-retrieving-access-token-typeerror-cannot-read-property-project-id-of-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!