使用带有 Raspberry Pi 3 和 Python 的 Google 助手库时出错(不接受凭据)
Error using Google Assistant Library with Raspberry Pi 3 and Python (Credentials not accepted)
我成功地让 Google 的助手在我的 Raspberry Pi 上工作 3. 它通过使用
工作
google-assistant-demo --d device_id1234
现在我想使用 python 访问权限,以便在识别到特定命令时打开 on/off LED。不幸的是,我关注的页面不是免费的,所以我无法提供 link。我的代码中有问题的部分如下:
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--credentials', type=existing_file,
metavar='OAUTH2_CREDENTIALS_FILE',
default=os.path.join(
os.path.expanduser('~/.config'),
'google-oauthlib-tool',
'credentials.json'
),
help='Path to store and read OAuth2 credentials')
args = parser.parse_args()
with open(args.credentials, 'r') as f:
credentials = google.oauth2.credentials.Credentials(token=None,
**json.load(f))
with Assistant(credentials) as assistant:
print("Warte auf Hotword")
for event in assistant.start():
process_event(event, assistant)
当我 运行 脚本时
python assistant.py --credentials client_secret_blabla.json
它抛出错误
Traceback (most recent call last):
File "assistant.py", line 137, in <module>
main()
File "assistant.py", line 128, in main
**json.load(f)
TypeError: __init__() got an unexpected keyword argument 'installed'
我尝试重新安装所有内容,还重新下载了 json 文件。显然 json 没有被正确识别,但我不知道该怎么做。我的 JSON 看起来像这样:
{"installed":{"client_id":"XXX.apps.googleusercontent.com","project_id":"dotted-nature-182957","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"XXX","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
这是凭据 class 所期望的:
http://google-auth.readthedocs.io/en/latest/reference/google.oauth2.credentials.html
我希望你们能帮助我解决这个问题。非常感谢!
我刚刚弄明白了。问题是我使用了错误的 json 文件。我发现文档对此不太清楚。您必须交给python脚本的JSON文件是执行
时下载的文件
google-oauthlib-tool --client-secrets /home/pi/PREVIOUSLYDOWNLOADEDJSON.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless
默认位于/home/pi/.config/google-oauthlib-tool/.
我成功地让 Google 的助手在我的 Raspberry Pi 上工作 3. 它通过使用
工作google-assistant-demo --d device_id1234
现在我想使用 python 访问权限,以便在识别到特定命令时打开 on/off LED。不幸的是,我关注的页面不是免费的,所以我无法提供 link。我的代码中有问题的部分如下:
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--credentials', type=existing_file,
metavar='OAUTH2_CREDENTIALS_FILE',
default=os.path.join(
os.path.expanduser('~/.config'),
'google-oauthlib-tool',
'credentials.json'
),
help='Path to store and read OAuth2 credentials')
args = parser.parse_args()
with open(args.credentials, 'r') as f:
credentials = google.oauth2.credentials.Credentials(token=None,
**json.load(f))
with Assistant(credentials) as assistant:
print("Warte auf Hotword")
for event in assistant.start():
process_event(event, assistant)
当我 运行 脚本时
python assistant.py --credentials client_secret_blabla.json
它抛出错误
Traceback (most recent call last):
File "assistant.py", line 137, in <module>
main()
File "assistant.py", line 128, in main
**json.load(f)
TypeError: __init__() got an unexpected keyword argument 'installed'
我尝试重新安装所有内容,还重新下载了 json 文件。显然 json 没有被正确识别,但我不知道该怎么做。我的 JSON 看起来像这样:
{"installed":{"client_id":"XXX.apps.googleusercontent.com","project_id":"dotted-nature-182957","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"XXX","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
这是凭据 class 所期望的: http://google-auth.readthedocs.io/en/latest/reference/google.oauth2.credentials.html
我希望你们能帮助我解决这个问题。非常感谢!
我刚刚弄明白了。问题是我使用了错误的 json 文件。我发现文档对此不太清楚。您必须交给python脚本的JSON文件是执行
时下载的文件google-oauthlib-tool --client-secrets /home/pi/PREVIOUSLYDOWNLOADEDJSON.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless
默认位于/home/pi/.config/google-oauthlib-tool/.