ग्राहकों के लिए Python क्विकस्टार्ट

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

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

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

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

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

  1. Google Developers Console में कोई प्रोजेक्ट बनाने या चुनने के लिए, इस विज़र्ड का इस्तेमाल करें और एपीआई को अपने-आप चालू करें. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं .
  2. 'क्रेडेंशियल बनाएं' पर रद्द करें पर क्लिक करें.
  3. पेज के सबसे ऊपर, OAuth की सहमति वाली स्क्रीन टैब चुनें. ईमेल पता चुनें, अगर पहले से सेट न किया गया हो, तो प्रॉडक्ट का नाम डालें और सेव करें बटन पर क्लिक करें.
  4. क्रेडेंशियल टैब चुनें. इसके बाद, क्रेडेंशियल बनाएं बटन पर क्लिक करें और OAuth क्लाइंट आईडी चुनें.
  5. ऐप्लिकेशन टाइप अन्य चुनें और "क्विकस्टार्ट" नाम डालें. इसके बाद, बनाएं बटन पर क्लिक करें.
  6. OAuth क्लाइंट पैनल को खारिज करने के लिए, ठीक है पर क्लिक करें.
  7. JSON डाउनलोड करें पर क्लिक करें.
  8. फ़ाइल को अपनी काम करने की डायरेक्ट्री में ले जाएं और उसका नाम बदलें client_secret.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 import tools
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage

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


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

  Ask the user to authorize the request using their Google Account in their
  browser. Because this method stores the cedential in the
  USER_CREDENTIAL_FILE, the user is typically only asked to the first time they
  run the script.

  Returns:
    Credentials, the user's credential.
  """
  flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  storage = Storage(USER_CREDENTIAL_FILE)
  credential = storage.get()

  if not credential or credential.invalid:
    credential = tools.run_flow(flow, storage)  # skipping flags for brevity
  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()

चौथा चरण: सैंपल चलाएं

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

python quickstart.py

पहली बार ऐप्लिकेशन चलाने पर, आपको इन चीज़ों का ऐक्सेस देना होगा:

  1. ऐप्लिकेशन आपके डिफ़ॉल्ट ब्राउज़र में एक नया टैब खोलने की कोशिश करता है. अगर ऐसा नहीं होता है, तो कंसोल से यूआरएल कॉपी करें और उसे अपने ब्राउज़र में खोलें. अगर आपने पहले से अपने Google खाते में लॉग इन नहीं किया है, तो आपको लॉग इन करने के लिए कहा जाता है. अगर आपने एक से ज़्यादा Google खातों में लॉग इन किया हुआ है, तो पेज पर आपको अनुमति के लिए एक खाता चुनने के लिए कहा जाएगा.
  2. स्वीकार करें पर क्लिक करें.
  3. ब्राउज़र टैब बंद करें—ऐप्लिकेशन चलता रहेगा.

नोट

  • Google API क्लाइंट लाइब्रेरी, फ़ाइल सिस्टम पर अनुमति वाला डेटा सेव करती है. इसलिए, आने वाले समय में लॉन्च होने पर, आपको अनुमति देने के लिए नहीं कहा जाता है.
  • ऐप्लिकेशन की अनुमति वाला डेटा रीसेट करने के लिए, user_credential.json फ़ाइल मिटाएं और ऐप्लिकेशन को फिर से चलाएं.
  • इस क्विकस्टार्ट में अनुमति फ़्लो, कमांड-लाइन ऐप्लिकेशन के लिए बेहतर है. किसी वेब ऐप्लिकेशन में अनुमति जोड़ने का तरीका जानने के लिए, वेब सर्वर ऐप्लिकेशन के लिए OAuth 2.0 का इस्तेमाल करना लेख देखें.

समस्या हल करना

यहां कुछ सामान्य बातें बताई गई हैं, जिन्हें आप देखना चाहेंगे. हमें बताएं कि क्या गड़बड़ी हुई और हम इसे ठीक करने की कोशिश करेंगे.

  • जांच लें कि आपने उसी Google खाते से एपीआई कॉल की अनुमति दी है जो पहले से तैयार आपके ग्राहक खाते का सदस्य है. अपने ऐक्सेस की जांच करने के लिए, पहले से मौजूद Google खाते का इस्तेमाल करके, पहले से तैयार डिवाइस पोर्ट में साइन इन करें.
  • पुष्टि करें कि खाते ने पोर्टल में जाकर, सेवा की नई शर्तों को स्वीकार किया है. ग्राहक खाते देखें.

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