非常类似于这个问题:ValueError:客户端机密必须用于web或已安装的应用程序,但有一点扭曲:我正试图通过Google虚拟机来完成这个任务。
最近,带外流(OOB)停止了对我的工作(似乎原因可能在这里:oob-迁移。在此之前,我能够轻松地在虚拟机上运行Google来读取/写入Google文件
现在,我试图遵循这个google工作表的Python快速启动,它几乎与我已经拥有的代码相同,在"Configure the sample"
部分下面。
我现在在Python上的代码是:
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', scopes)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentials_path, scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
#Store creds in object
my_creds = creds
#Create service
build('sheets', 'v4', credentials=my_creds)
但是每次遇到这个错误:
ValueError: Client secrets must be for a web or installed app.
为了记录在案,我在Google上创建了"OAuth 2.0 Client IDs"
下的凭据,应用程序类型为"Web application"
。如果不是那种类型的话,我不知道该选哪种。
非常感谢你的帮助,真的很感激。
发布于 2022-12-03 10:22:44
您正在使用的代码是为已安装的代码设计的。这正是你的错误信息所说的。QuickStart清楚地指出了Click Application type > Desktop app
。
虽然我同意错误消息状态已安装或web,但我不确定代码是否可以用于web应用程序。
客户端机密必须是一个网页或已安装的应用程序。
打开由credentials_path表示的文件,文件应该具有以下格式。
credentials.json
{
"installed": {
"client_id": "[redacted]",
"project_id": "daimto-tutorials-101",
"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": "[redacted]",
"redirect_uris": [
"http://localhost"
]
}
}
点检查。
urn:ietf:wg:oauth:2.0:oob
的内容下面是一个视频,它将向您展示如何创建与该代码一起使用的正确的凭据文件。这应该通过谷歌云控制台来完成
注意:我非常怀疑这个问题是由于oob造成的,如果是的话,您可能会收到不同的错误信息。
更新Web应用程序工作。
我能够用一个web应用程序凭证来测试这个。我唯一需要做的更改是表示我希望运行代码的端口,以便获得一个静态端口,我需要将uri重定向到developer控制台项目。
我没有对标准的快速起跑做任何其他的改变。
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIALS_FILE_PATH, SCOPES)
creds = flow.run_local_server(port=53911)
https://stackoverflow.com/questions/74663524
复制相似问题