리셀러를 위한 Python 빠른 시작

이 빠른 시작 가이드의 단계를 따르면 약 10분 후에 제로터치 등록 리셀러 API에 요청하는 간단한 Python 명령줄 앱을 사용할 수 있습니다.

기본 요건

이 빠른 시작을 실행하려면 다음이 필요합니다.

  • 제로터치 등록 리셀러 계정의 구성원인 Google 계정 아직 온보딩하지 않은 경우 리셀러 포털 가이드시작하기 단계를 따릅니다.
  • Python 2.6 이상
  • pip 패키지 관리 도구
  • 인터넷 및 웹브라우저 액세스

1단계: 제로터치 등록 API 사용 설정하기

  1. 이 마법사를 사용하여 Google Developers Console에서 프로젝트를 만들거나 선택하고 API를 자동으로 사용 설정하세요. 계속을 클릭한 다음 사용자 인증 정보로 이동을 클릭합니다.
  2. 어떤 데이터에 액세스하시겠습니까?애플리케이션 데이터로 설정합니다.
  3. 다음을 클릭합니다. 서비스 계정을 만들라는 메시지가 표시됩니다.
  4. 서비스 계정 이름을 설명하는 이름을 입력합니다.
  5. 나중에 사용할 수 있도록 서비스 계정 ID(이메일 주소 모양)를 기록해 둡니다.
  6. 역할서비스 계정 > 서비스 계정 사용자로 설정합니다.
  7. 완료를 클릭하여 서비스 계정 만들기를 마칩니다.
  8. 만든 서비스 계정의 이메일 주소를 클릭합니다.
  9. **키**를 클릭합니다.
  10. **키 추가**를 클릭한 다음 **새 키 만들기**를 클릭합니다.
  11. **키 유형**에서 **JSON**을 선택합니다.
  12. 만들기를 클릭하면 비공개 키가 컴퓨터에 다운로드됩니다.
  13. **닫기**를 클릭합니다.
  14. 파일을 작업 디렉터리로 이동하고 이름을 service_account_key.json로 변경합니다.
  1. 제로터치 등록 포털을 엽니다. 로그인해야 할 수도 있습니다.
  2. 서비스 계정을 클릭합니다.
  3. 서비스 계정 연결을 클릭합니다.
  4. 이메일 주소를 앞서 만든 서비스 계정의 주소로 설정합니다.
  5. 서비스 계정 연결을 클릭하여 제로터치 등록 계정으로 서비스 계정을 사용합니다.

3단계: Google 클라이언트 라이브러리 설치하기

다음 명령어를 실행하여 pip를 사용하여 라이브러리를 설치합니다.

pip install --upgrade google-api-python-client

다양한 설치 옵션은 라이브러리의 설치 페이지를 참고하세요.

4단계: 샘플 설정

작업 디렉터리에 quickstart.py이라는 파일을 만듭니다. 다음 코드를 복사하여 파일을 저장합니다. 자체 리셀러 파트너 IDPARTNER_ID 값(가져온 후 앱의 첫 번째 줄)으로 삽입합니다.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Zero-touch enrollment reseller quickstart.

This script forms the quickstart introduction to the zero-touch enrollemnt
reseller API. To learn more, visit https://developer.google.com/zero-touch
"""

from apiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

# TODO: replace this with your partner reseller ID.
PARTNER_ID = '11036885';

# A single auth scope is used for the zero-touch enrollment customer API.
SCOPES = ['https://www.googleapis.com/auth/androidworkprovisioning']
SERVICE_ACCOUNT_KEY_FILE = 'service_account_key.json'

def get_credential():
  """Creates a Credential object with the correct OAuth2 authorization.

  Creates a Credential object with the correct OAuth2 authorization
  for the service account that calls the reseller API. The service
  endpoint calls this method when setting up a new service instance.

  Returns:
    Credential, the user's credential.
  """
  credential = ServiceAccountCredentials.from_json_keyfile_name(
      SERVICE_ACCOUNT_KEY_FILE, scopes=SCOPES)
  return credential


def get_service():
  """Creates a service endpoint for the zero-touch enrollment reseller API.

  Builds and returns an authorized API client service for v1 of the API. Use
  the service endpoint to call the API methods.

  Returns:
    A service Resource object with methods for interacting with the service.
  """
  http_auth = get_credential().authorize(Http())
  service = build('androiddeviceprovisioning', 'v1', http=http_auth)
  return service


def main():
  """Runs the zero-touch enrollment quickstart app.
  """
  # Create a zero-touch enrollment API service endpoint.
  service = get_service()

  # Send an API request to list all our customers.
  response = service.partners().customers().list(partnerId=PARTNER_ID).execute()

  # Print out the details of each customer.
  if 'customers' in response:
    for customer in response['customers']:
      print 'Name:{0}  ID:{1}'.format(
          customer['companyName'], customer['companyId'])
  else:
    print 'No customers found'


if __name__ == '__main__':
  main()

파트너 ID

API 호출에는 일반적으로 리셀러 파트너 ID가 인수로 필요합니다. 제로터치 등록 포털에서 파트너 ID를 찾으려면 다음 단계를 따르세요.

  1. 포털을 엽니다. 로그인해야 할 수도 있습니다.
  2. 서비스 계정을 클릭합니다.
  3. 리셀러 ID 행에서 파트너 ID 번호를 복사합니다.

5단계: 샘플 실행

운영체제의 도움말을 사용하여 파일에서 스크립트를 실행합니다. UNIX 및 Mac 컴퓨터의 경우 터미널에서 아래 명령어를 실행합니다.

python quickstart.py

API 응답 인쇄

API를 사용해 볼 때 응답을 더 쉽게 검사하려면 JSON 응답 데이터의 형식을 지정합니다. 아래 스니펫은 JSON 모듈을 사용하여 Python에서 수행하는 방법을 보여줍니다.

from json import dumps

# ...

results = provisioning.partners().devices().claimAsync(partnerId=MY_PARTNER_ID,
 body={'claims':new_claims}).execute()
# Print formatted JSON response
print dumps(results, indent=4, sort_keys=True)

문제 해결

빠른 시작에 어떤 문제가 발생했는지 알려주시면 해결하도록 노력하겠습니다. 제로터치가 서비스 계정을 사용하여 API 호출을 승인하는 방법을 알아보려면 승인을 읽어보세요.

자세히 알아보기