首页 > 解决方案 > 检索访问令牌时出错:TypeError:无法读取未定义的属性“project_id”

问题描述

我是新手。

通过以下方式初始登录后: clasp login 我能够登录到 script.google.com 接下来,我创建了一个项目并通过以下方式推送文件: clasp push

现在,我已使用以下命令注销: clasp logout

这里需要帮助:现在,如果我正在尝试:

clasp 登录 --creds ./.clasp.json

我收到“检索访问令牌时出错:TypeError:无法读取未定义的属性 'project_id'”。

请指导我如何通过 --creds 登录?

标签: clasp

解决方案


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.


推荐阅读