Python क्विकस्टार्ट

क्विकस्टार्ट में, Google Workspace API को कॉल करने वाले ऐप्लिकेशन को सेट अप और चलाने का तरीका बताया गया है. इस क्विकस्टार्ट में, पुष्टि करने का आसान तरीका इस्तेमाल किया गया है. यह टेस्टिंग एनवायरमेंट के लिए सही है. हमारा सुझाव है कि प्रोडक्शन एनवायरमेंट के लिए, ऐक्सेस क्रेडेंशियल चुनने से पहले, पुष्टि करने और अनुमति देने के बारे में जान लें. इससे आपको अपने ऐप्लिकेशन के लिए सही क्रेडेंशियल चुनने में मदद मिलेगी.

एक Python कमांड-लाइन ऐप्लिकेशन बनाएं, जो Drive Labels API को अनुरोध भेजता हो.

मकसद

  • अपना एनवायरमेंट सेट अप करें.
  • क्लाइंट लाइब्रेरी इंस्टॉल करें.
  • सैंपल सेट अप करें.
  • सैंपल चलाएं.

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

  • एक Google खाता.

अपना एनवायरमेंट सेट अप करने का तरीका

इस क्विकस्टार्ट को पूरा करने के लिए, अपना एनवायरमेंट सेट अप करें.

एपीआई चालू करना

Google API का इस्तेमाल करने से पहले, आपको उन्हें Google Cloud प्रोजेक्ट में चालू करना होगा. एक ही Google Cloud प्रोजेक्ट में, एक या उससे ज़्यादा एपीआई चालू किए जा सकते हैं.

डेस्कटॉप ऐप्लिकेशन के लिए क्रेडेंशियल को अनुमति देना

असली उपयोगकर्ताओं की पुष्टि करने और अपने ऐप्लिकेशन में उपयोगकर्ता का डेटा ऐक्सेस करने के लिए, आपको एक या उससे ज़्यादा OAuth 2.0 क्लाइंट आईडी बनाने होंगे. क्लाइंट आईडी का इस्तेमाल, Google के OAuth सर्वर पर किसी एक ऐप्लिकेशन की पहचान करने के लिए किया जाता है. अगर आपका ऐप्लिकेशन कई प्लैटफ़ॉर्म पर चलता है, तो आपको हर प्लैटफ़ॉर्म के लिए अलग क्लाइंट आईडी बनाना होगा.
  1. Google Cloud console में, मेन्यू > Google Auth platform > क्लाइंट पर जाएं.

    क्लाइंट पर जाएं

  2. क्लाइंट बनाएं पर क्लिक करें.
  3. ऐप्लिकेशन का टाइप > डेस्कटॉप ऐप्लिकेशन पर क्लिक करें.
  4. नाम फ़ील्ड में, क्रेडेंशियल के लिए कोई नाम टाइप करें. यह नाम सिर्फ़ Google Cloud Console में दिखता है.
  5. बनाएं पर क्लिक करें.

    नया क्रेडेंशियल, "OAuth 2.0 क्लाइंट आईडी" में दिखता है.

  6. डाउनलोड की गई JSON फ़ाइल को credentials.json के तौर पर सेव करें. इसके बाद, फ़ाइल को अपनी वर्किंग डायरेक्ट्री में ले जाएं.

Google क्लाइंट लाइब्रेरी इंस्टॉल करना

  • Python के लिए Google क्लाइंट लाइब्रेरी इंस्टॉल करें:

      pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    

इंस्टॉल करने के अन्य विकल्पों के लिए, Python लाइब्रेरी के इंस्टॉल करने से जुड़े सेक्शन पर जाएं.

सैंपल को कॉन्फ़िगर करना

  1. अपनी वर्किंग डायरेक्ट्री में, quickstart.py नाम की एक फ़ाइल बनाएं.
  2. quickstart.py में यह कोड शामिल करें:

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # If modifying these scopes, delete the file token.json.
    SCOPES = ['https://www.googleapis.com/auth/drive.labels.readonly']
    
    def main():
      """Shows basic usage of the Drive Labels API.
    
        Prints the first page of the customer's Labels.
      """
      creds = None
      # The file token.json stores the user's access and refresh tokens, and is
      # created automatically when the authorization flow completes for the first
      # time.
      if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
      # If there are no (valid) credentials available, let the user log in.
      if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
          creds.refresh(Request())
        else:
          flow = InstalledAppFlow.from_client_secrets_file('credentials.json',
                                                          SCOPES)
          creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
          token.write(creds.to_json())
      try:
        service = build('drivelabels', 'v2', credentials=creds)
        response = service.labels().list(
            view='LABEL_VIEW_FULL').execute()
        labels = response['labels']
    
        if not labels:
          print('No Labels')
        else:
          for label in labels:
            name = label['name']
            title = label['properties']['title']
            print(u'{0}:\t{1}'.format(name, title))
      except HttpError as error:
        # TODO (developer) - Handle errors from Labels API.
        print(f'An error occurred: {error}')
    
    if __name__ == '__main__':
      main()
    

सैंपल चलाना

  1. अपनी वर्किंग डायरेक्ट्री में, सैंपल बनाएं और उसे चलाएं:

    python quickstart.py
    
  2. पहली बार सैंपल चलाने पर, आपको ऐक्सेस की अनुमति देने के लिए कहा जाएगा:

    1. अगर आपने Google खाते में पहले से साइन इन नहीं किया है, तो आपको साइन इन करने के लिए कहा जाएगा. अगर आपने एक से ज़्यादा खातों में साइन इन किया हुआ है, तो पुष्टि के लिए किसी एक खाते को चुनें.
    2. स्वीकार करें पर क्लिक करें.

    अनुमति से जुड़ी जानकारी फ़ाइल सिस्टम में सेव होती है. इसलिए, अगली बार सैंपल कोड चलाने पर, आपको अनुमति देने के लिए नहीं कहा जाएगा.

आपने Drive Labels API से अनुरोध करने वाला अपना पहला Python ऐप्लिकेशन बना लिया है.

अगले चरण