Jekson
Jekson

Reputation: 3252

Valid authentication credential for Android Management API

I'm learning to work with api and now I try to get a list of devices runing a python script from the console. Client Library for Python installed.

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)

And I got error

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.">

What do I need to add to the code to automatically authenticate?

Upvotes: 1

Views: 611

Answers (1)

Jekson
Jekson

Reputation: 3252

Having drawn conclusions from the tips @tehhowch, I found a solution for my question.

Instead ApiKey need using OAuth2ServiceAccount. More informations at this page. Sample code for my task:

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)

The SCOPES parameter can be selected individually from this document.

Upvotes: 4

Related Questions