TLDR:您正在使用配置文件(.clasp.json
),而不是凭证文件(creds.json
或其他)从 Google Cloud 项目控制台。
当您登录时,凭据的默认存储位于名为的文件中.clasprc.json
in the ~
目录 (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).
请注意,该文件(.clasprc.json
) 不等于.clasp.json
.
clasprc.json https://github.com/google/clasp/blob/2.0.1/src/auth.ts#L19-L43 format:
据称,该文件的内容取决于身份验证类型(全局或本地):
// 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
}
本地身份验证存储客户端机密/等,如果您打算使用,通常需要clasp run https://github.com/google/clasp/tree/2.0.1#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
}
(实际上,两个文件的格式都是LOCAL
文件--属性token
, oauth2ClientSettings
, and isLocalCreds
——虽然价值isLocalCreds
对于全局登录将为 false。)
扣子.json https://github.com/google/clasp/blob/2.0.1/README.md#project-settings-file-claspjson format:
{
"scriptId": "",
"rootDir": "build/",
"projectId": "project-id-xxxxxxxxxxxxxxxxxxx",
"fileExtension": "ts",
"filePushOrder": ["file1.ts", "file2.ts"]
}
注意clasp.json
is 脚本文件的配置 and clasprc.json
is stored 用户的凭证/授权. Neither其中是用于本地登录的相应凭证文件。
解决错误
您收到的特定错误是由于您提供了不正确的文件而导致的。您提供的“凭据”文件不具有所需的属性,因此当 clasp 尝试从该属性读取 https://github.com/google/clasp/blob/b5701c70be348fd9d2e2b4eb85e670c3298d9e6e/src/auth.ts#L92
console.log(LOG.CREDS_FROM_PROJECT(options.creds.installed.project_id));
你得到错误:
检索访问令牌时出错: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"]
}
}
如果您的凭据文件不具有该格式,则无法使用它在本地登录。