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

क्विकस्टार्ट, ऐसे ऐप्लिकेशन को सेट अप करने और चलाने का तरीका बताता है जो Google Workspace API.

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

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

मकसद

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

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

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

  • एक Google खाता.

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

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

इस एपीआई को चालू करें

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

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

  1. Google Cloud Console में, मेन्यू पर जाएं > एपीआई और सेवाएं > OAuth की सहमति वाली स्क्रीन.

    उस स्क्रीन पर जाएं जहां OAuth के लिए सहमति दी जाती है

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

  5. अपने ऐप्लिकेशन रजिस्ट्रेशन की खास जानकारी देखें. बदलाव करने के लिए, बदलाव करें पर क्लिक करें. अगर ऐप्लिकेशन पंजीकरण के लिए ठीक लगता है, तो डैशबोर्ड पर वापस जाएं पर क्लिक करें.

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

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

    क्रेडेंशियल पर जाएं

  2. क्रेडेंशियल बनाएं > OAuth क्लाइंट आईडी पर क्लिक करें.
  3. ऐप्लिकेशन टाइप > डेस्कटॉप ऐप्लिकेशन पर क्लिक करें.
  4. नाम फ़ील्ड में, क्रेडेंशियल के लिए कोई नाम लिखें. यह नाम सिर्फ़ Google Cloud Console में दिखता है.
  5. बनाएं पर क्लिक करें. OAuth क्लाइंट की बनाई गई स्क्रीन दिखेगी. इसमें आपका नया क्लाइंट आईडी और क्लाइंट सीक्रेट दिखेगा.
  6. ठीक है पर क्लिक करें. नया क्रेडेंशियल, OAuth 2.0 क्लाइंट आईडी के तहत दिखता है.
  7. डाउनलोड की गई JSON फ़ाइल को credentials.json के तौर पर सेव करें और फ़ाइल को अपनी वर्किंग डायरेक्ट्री में सेव कर सकते हैं.

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

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

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

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

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

    Drive/activity-v2/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.activity.readonly"]
    
    
    def main():
      """Shows basic usage of the Drive Activity API.
    
      Prints information about the last 10 events that occured the user's Drive.
      """
      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())
    
      service = build("driveactivity", "v2", credentials=creds)
    
      # Call the Drive Activity API
      try:
        results = service.activity().query(body={"pageSize": 10}).execute()
        activities = results.get("activities", [])
    
        if not activities:
          print("No activity.")
        else:
          print("Recent activity:")
          for activity in activities:
            time = getTimeInfo(activity)
            action = getActionInfo(activity["primaryActionDetail"])
            actors = map(getActorInfo, activity["actors"])
            targets = map(getTargetInfo, activity["targets"])
            actors_str, targets_str = "", ""
            actor_name = actors_str.join(actors)
            target_name = targets_str.join(targets)
    
            # Print the action occurred on drive with actor, target item and timestamp
            print(f"{time}: {action}, {actor_name}, {target_name}")
    
      except HttpError as error:
        # TODO(developer) - Handleerrors from drive activity API.
        print(f"An error occurred: {error}")
    
    
    # Returns the name of a set property in an object, or else "unknown".
    def getOneOf(obj):
      for key in obj:
        return key
      return "unknown"
    
    
    # Returns a time associated with an activity.
    def getTimeInfo(activity):
      if "timestamp" in activity:
        return activity["timestamp"]
      if "timeRange" in activity:
        return activity["timeRange"]["endTime"]
      return "unknown"
    
    
    # Returns the type of action.
    def getActionInfo(actionDetail):
      return getOneOf(actionDetail)
    
    
    # Returns user information, or the type of user if not a known user.
    def getUserInfo(user):
      if "knownUser" in user:
        knownUser = user["knownUser"]
        isMe = knownUser.get("isCurrentUser", False)
        return "people/me" if isMe else knownUser["personName"]
      return getOneOf(user)
    
    
    # Returns actor information, or the type of actor if not a user.
    def getActorInfo(actor):
      if "user" in actor:
        return getUserInfo(actor["user"])
      return getOneOf(actor)
    
    
    # Returns the type of a target and an associated title.
    def getTargetInfo(target):
      if "driveItem" in target:
        title = target["driveItem"].get("title", "unknown")
        return f'driveItem:"{title}"'
      if "drive" in target:
        title = target["drive"].get("title", "unknown")
        return f'drive:"{title}"'
      if "fileComment" in target:
        parent = target["fileComment"].get("parent", {})
        title = parent.get("title", "unknown")
        return f'fileComment:"{title}"'
      return f"{getOneOf(target)}:unknown"
    
    
    if __name__ == "__main__":
      main()

सैंपल चलाएं

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

    python3 quickstart.py
    
  1. पहली बार सैंपल चलाने पर, आपको ऐक्सेस की अनुमति देने के लिए कहा जाता है:
    1. अगर आपने अपने Google खाते में पहले से साइन इन नहीं किया है, तो मांगे जाने पर साइन इन करें. अगर आपने अगर आपने कई खातों में साइन इन किया है, तो अनुमति देने के लिए एक खाता चुनें.
    2. स्वीकार करें पर क्लिक करें.

    आपका Python ऐप्लिकेशन, Google Drive Activity API को चलाता है और उसे कॉल करता है.

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

अगले चरण