किसी सेवा खाते का इस्तेमाल करने वाले ग्राहकों के लिए Python क्विकस्टार्ट

इस क्विकस्टार्ट गाइड में दिए गए निर्देशों का पालन करें और करीब 10 मिनट में आपके पास एक साधारण Python कमांड-लाइन ऐप्लिकेशन हो. यह ऐप्लिकेशन, सेवा खाते का इस्तेमाल करके, पहले से तैयार डिवाइस ग्राहक एपीआई के लिए अनुरोध करता है.

ज़रूरी शर्तें

इस क्विकस्टार्ट को चलाने के लिए, आपको इनकी ज़रूरत होगी:

  • एक सेवा खाता जो पहले से तैयार डिवाइस वाले ग्राहक खाते से जुड़ा हो. शुरू करें देखें.
  • Python 3.0 या इसके बाद का वर्शन.
  • पीआईपी पैकेज मैनेजमेंट टूल.
  • इंटरनेट और वेब ब्राउज़र का ऐक्सेस.

पहला चरण: पहले से तैयार डिवाइस के लिए एपीआई चालू करना

  1. Google Developers Console में कोई प्रोजेक्ट बनाने या चुनने के लिए, इस विज़र्ड का इस्तेमाल करें और एपीआई को अपने-आप चालू करें. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं .
  2. आपको कौनसा डेटा ऐक्सेस करना है? को ऐप्लिकेशन डेटा पर सेट करें.
  3. आगे बढ़ें पर क्लिक करें. आपको सेवा खाता बनाने के लिए कहा जाएगा.
  4. सेवा खाते के नाम की जानकारी देने वाला नाम दें.
  5. सेवा खाता आईडी देखें (यह एक ईमेल पते जैसा लगता है) क्योंकि आप बाद में इसका इस्तेमाल करेंगे.
  6. भूमिका को सेवा खाते > सेवा खाते के उपयोगकर्ता पर सेट करें.
  7. सेवा खाता बनाने की प्रोसेस पूरी करने के लिए, हो गया पर क्लिक करें.
  8. अपने बनाए गए सेवा खाते के ईमेल पते पर क्लिक करें.
  9. **बटन** पर क्लिक करें.
  10. **कुंजी जोड़ें** पर क्लिक करें. इसके बाद, **नई कुंजी बनाएं** पर क्लिक करें.
  11. **कुंजी टाइप** के लिए, **JSON** को चुनें.
  12. अपने कंप्यूटर पर, बनाएं और निजी कुंजी के डाउनलोड की जानकारी पर क्लिक करें.
  13. **बंद करें** पर क्लिक करें.
  14. फ़ाइल को अपनी काम करने की डायरेक्ट्री में ले जाएं और उसका नाम बदलें service_account_key.json.

दूसरा चरण: Google क्लाइंट लाइब्रेरी इंस्टॉल करना

पीआईपी का इस्तेमाल करके लाइब्रेरी इंस्टॉल करने के लिए, यह निर्देश चलाएं:

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

लाइब्रेरी के अलग-अलग विकल्पों के बारे में जानने के लिए, लाइब्रेरी का इंस्टॉलेशन पेज देखें.

तीसरा चरण: सैंपल सेट अप करना

अपनी काम करने की डायरेक्ट्री में quickstart.py नाम की फ़ाइल बनाएं. नीचे दिए गए कोड को कॉपी करें और फ़ाइल सेव करें.

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

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

import sys
from apiclient import discovery
import httplib2
from oauth2client.service_account import ServiceAccountCredentials

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


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

  Uses the service account key stored in SERVICE_ACCOUNT_KEY_FILE.

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

  if not credential or credential.invalid:
    print('Unable to authenticate using service account key.')
    sys.exit()
  return credential


def get_service():
  """Creates a service endpoint for the zero-touch enrollment 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(httplib2.Http())
  return discovery.build('androiddeviceprovisioning', 'v1', http=http_auth)


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

  # Get the customer's account. Because a customer might have more
  # than one, limit the results to the first account found.
  response = service.customers().list(pageSize=1).execute()

  if 'customers' not in response:
    # No accounts found for the user. Confirm the Google Account
    # that authorizes the request can access the zero-touch portal.
    print('No zero-touch enrollment account found.')
    sys.exit()
  customer_account = response['customers'][0]['name']

  # Send an API request to list all the DPCs available using the customer
  # account.
  results = service.customers().dpcs().list(parent=customer_account).execute()

  # Print out the details of each DPC.
  for dpc in results['dpcs']:
    # Some DPCs may not have a name, so replace with a marker.
    if 'dpcName' in dpc:
      dpcName = dpc['dpcName']
    else:
      dpcName = "-"
    print('Name:{0}  APK:{1}'.format(dpcName, dpc['packageName']))


if __name__ == '__main__':
  main()

चौथा चरण: अपनी सेवा खाते की कुंजी जोड़ना

अपना सेवा खाता बनाते समय, service_account_key.json को अपनी काम करने की डायरेक्ट्री में कॉपी करें.

पांचवां चरण: सैंपल चलाना

फ़ाइल में स्क्रिप्ट चलाने के लिए, अपने ऑपरेटिंग सिस्टम की मदद लें. UNIX और Mac कंप्यूटर पर, अपने टर्मिनल में नीचे दिया गया कमांड चलाएं:

python quickstart.py

नोट

  • अपनी service_account_key.json फ़ाइल किसी के साथ शेयर करने से बचें. ध्यान रखें कि इसे सोर्स कोड के डेटा स्टोर करने की जगहों में शामिल न किया जाए. ज़्यादा जानकारी के लिए, सेवा खाते के सीक्रेट मैनेज करने से जुड़ी सलाह पढ़ें.

ज़्यादा जानें