【问题标题】:Valid authentication credential for Android Management APIAndroid Management API 的有效身份验证凭据
【发布时间】:2019-03-13 13:56:13
【问题描述】:

我正在学习使用 api,现在我尝试从控制台获取运行 python 脚本的设备列表。已安装 Python 客户端库。

sample.py

import pprint
import sys
from apiclient.discovery import build
import json


api_key = 'AIzaSyBNv8k-zm_TkytbBMJVkR7_wjc'


service = build('androidmanagement', 'v1', developerKey=api_key)


response = service.enterprises().devices().list(parent="enterprises/LC03w9087868").execute()

pprint.pprint(response)

我得到了错误

HttpError 401 when requesting https://androidmanagement.googleapis.com/v1/enterprises/LC03w9087868/devices?key=AIzaSyBNv8k-zm_TkytbBMJVkR7_wjc&alt=json returned "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.">

我需要在代码中添加什么来自动进行身份验证?

【问题讨论】:

  • API 密钥是有效的凭证吗?该错误表明它不是。
  • @tehhowch 我试过用新的钥匙,但结果是一样的。
  • 您是否查看过任何官方文档,尤其是快速入门或示例页面?您是否查看了developerKey 参数对 Python 客户端库的含义? (提示:API 密钥不足以授权您)
  • @tehhowch 我明白了,但你能给我更多信息或代码示例吗?
  • 转到文档。查看快速入门和示例页面。他们有工作代码示例!

标签: android python google-api google-api-python-client android-management-api


【解决方案1】:

从提示 @tehhowch 中得出结论后,我为我的问题找到了解决方案。

而 ApiKey 需要使用 OAuth2ServiceAccount。更多信息请访问page。我的任务的示例代码:

import pprint
from apiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/androidmanagement']
SERVICE_ACCOUNT_FILE = '//home/y700/Downloads/Cupla-v3.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)


service = build('androidmanagement', 'v1', credentials=credentials)
response = service.enterprises().devices().list(parent="enterprises/LC0XXXXXX98").execute()

pprint.pprint(response)

SCOPES 参数可以从这个document 中单独选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 2015-12-20
    • 2014-10-15
    • 2021-07-03
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多