我是新手。
通过以下方式初始登录后: clasp login 我能够登录到 script.google.com 接下来,我创建了一个项目并通过以下方式推送文件: clasp push
现在,我已使用以下命令注销: clasp logout
这里需要帮助:现在,如果我正在尝试:
clasp 登录 --creds ./.clasp.json
我收到“检索访问令牌时出错:TypeError:无法读取未定义的属性 'project_id'”。
请指导我如何通过 --creds 登录?
我是新手。
通过以下方式初始登录后: clasp login 我能够登录到 script.google.com 接下来,我创建了一个项目并通过以下方式推送文件: clasp push
现在,我已使用以下命令注销: clasp logout
这里需要帮助:现在,如果我正在尝试:
clasp 登录 --creds ./.clasp.json
我收到“检索访问令牌时出错:TypeError:无法读取未定义的属性 'project_id'”。
请指导我如何通过 --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.
当我错误地为 oauth 类型选择“Web 应用程序”而不是“桌面应用程序”时,我也遇到了这个错误,在这种情况下,主要部分是“web”,而不是“已安装”,我收到相同的
Error retrieving access token: TypeError: Cannot read property 'project_id' of undefined
错误的 oauth 客户端看起来像
{
"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>"
}
}
而正确的是
{
"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"
]
}
}
该脚本尝试解析“ installed.project_id ”变量并失败。