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

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

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

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

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

पहला चरण: ज़ीरो-टच रजिस्ट्रेशन एपीआई को चालू करना

  1. इसका उपयोग करें विज़र्ड पर क्लिक करें और एपीआई अपने-आप चालू हो जाएगा. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं .
  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()

चरण 4: सैंपल चलाएं

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

python quickstart.py

ऐप्लिकेशन को पहली बार इस्तेमाल करने के लिए, आपको ऐक्सेस की अनुमति देनी होगी:

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

नोट

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

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

यहां कुछ ऐसी सामान्य चीज़ें दी गई हैं, जिन्हें आप जांचना चाहेंगे. हमें बताएं कि क्विकस्टार्ट में क्या गड़बड़ी हुई और हम उसे ठीक करने के लिए काम करेंगे.

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

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