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

Nis*_*sal 4 clasp

我是扣扣新手。

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

现在,我已使用以下方式注销:clasp logout

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

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

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

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

teh*_*wch 7

TLDR:您使用的是配置文件 ( ),而不是Google Cloud 项目控制台中的.clasp.json凭据文件(或其他文件)。creds.json


登录时,凭据的默认存储位于目录.clasprc.json中指定的文件中~C:\Users\<user>\在 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).
Run Code Online (Sandbox Code Playgroud)

请注意,此文件 ( .clasprc.json) 与.clasp.json.

clasprc.json格式:

据称,该文件的内容取决于身份验证类型(全局或本地):

// 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
 }
Run Code Online (Sandbox Code Playgroud)

本地身份验证存储客户端机密等,如果您打算clasp run通过 Google Apps 脚本 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
}
Run Code Online (Sandbox Code Playgroud)

(实际上,这两个文件都将具有文件格式LOCAL--properties tokenoauth2ClientSettingsisLocalCreds--- 尽管对于全局登录来说 的值isLocalCreds将为 false。)

clasp.json格式:

{
  "scriptId": "",
  "rootDir": "build/",
  "projectId": "project-id-xxxxxxxxxxxxxxxxxxx",
  "fileExtension": "ts",
  "filePushOrder": ["file1.ts", "file2.ts"]
}
Run Code Online (Sandbox Code Playgroud)

请注意,这clasp.json脚本文件的配置,并clasprc.json存储用户的凭据/授权它们都不是适合本地登录的凭据文件。

解决错误

您收到的特定错误是由于您提供了不正确的文件而导致的。您提供的“凭据”文件不具有所需的属性,因此当 clasp 尝试从该属性读取时

  console.log(LOG.CREDS_FROM_PROJECT(options.creds.installed.project_id));
Run Code Online (Sandbox Code Playgroud)

你得到错误:

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

您可以从 Apps 脚本项目的 Google Cloud 项目页面获取正确的凭据文件,即https://console.cloud.google.com/apis/credentials?authuser=0&project=<some project id>

该文件的格式为:

{
    "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"]
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您的凭据文件不具有该格式,则无法使用它在本地登录。