क्विकस्टार्ट: Python में Search Console ऐप्लिकेशन चलाना

यह वेब ऐप्लिकेशन उन साइटों की सूची प्रिंट करता है जिन्हें आप ऐक्सेस कर सकते हैं. साथ ही, अगर कोई साइटमैप है, तो उन सभी साइटों के साइटमैप भी प्रिंट किए जाते हैं.

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

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

  • सैंपल ऐप्लिकेशन को मंज़ूरी देने के लिए, इंटरनेट और वेब ब्राउज़र का ऐक्सेस.
  • ऐसा Google खाता होना चाहिए जिसकी कम से कम एक वेबसाइट, Google Search Console में पुष्टि हो चुकी हो.
  • Python 3 और flask वेब ऐप्लिकेशन फ़्रेमवर्क.

निर्देश

इस सैंपल के लिए, आपको वेब सर्वर ऐप्लिकेशन के सैंपल ऐप्लिकेशन के लिए OAuth 2.0 का इस्तेमाल करना होगा. Python का यह नमूना, OAuth कुंजियों को मैनेज करने वाले और Google Cloud API को कॉल करने वाले वेब-आधारित ऐप्लिकेशन को चलाने के लिए, फ़्लास्क वेब ऐप्लिकेशन फ़्रेमवर्क का इस्तेमाल करता है. Search Console API को कॉल करने और वेब पेज पर नतीजों को प्रिंट करने के लिए, आपको लिंक किए गए सैंपल में बदलाव करना होगा.

ऊपर लिंक किए गए OAuth सैंपल पेज पर, सेटअप करने के निर्देशों का पालन करें और सैंपल कोड कॉपी करें. इसके बाद, नीचे बताए गए तरीके से कोड में बदलाव करें. खास तौर पर, अपने कोडिंग एनवायरमेंट के लिए सेटअप करने के निर्देशों का पालन करें. ऐसा प्रोजेक्ट सेट अप (या फिर से इस्तेमाल) करें जो Google Cloud Console में Search Console API को ऐक्सेस कर सके, और वेब ऐप्लिकेशन के लिए क्रेडेंशियल जनरेट कर सके.

इस सैंपल के लिए, सोर्स कोड के तौर पर Python के लिए, कंपलीट कोड के उदाहरण का इस्तेमाल किया जाएगा.

लिंक किए गए निर्देशों में आपको क्या बदलाव करने हैं, यह जानने के लिए यहां दिए गए बदलाव पढ़ें.

अगर आपको किसी Google App Engine प्रोजेक्ट से Google API Python Client को ऐक्सेस करना है, तो आपको अपनी अनुमतियां मैनेज करने के लिए सेवा खाते का इस्तेमाल करना होगा.

बदलाव

लिंक किए गए Oauth2 सैंपल पेज पर दिए गए निर्देशों का पालन करते समय, ये बदलाव करें:

  1. Drive API के बजाय, Google Search Console API को चालू करें.
  2. ऊपर लिंक किए गए, OAUth दस्तावेज़ के आखिर में दिए गए, सैंपल ऐप्लिकेशन को कॉपी करें. इसके बाद, इसे बदलें

    SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
    API_SERVICE_NAME = 'drive'
    API_VERSION = 'v2'
    
    इससे:
    SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
    API_SERVICE_NAME = 'searchconsole'
    API_VERSION = 'v1'
    

  3. Python भाषा test_api_request() फ़ंक्शन के मुख्य हिस्से को नीचे दिए गए कोड से बदलें:

    @app.route('/test')
    def test_api_request():
      if 'credentials' not in flask.session:
        return flask.redirect('authorize')
    
      # Load credentials from the session.
      credentials = google.oauth2.credentials.Credentials(
          **flask.session['credentials'])
    
      # Retrieve list of properties in account
      search_console_service = googleapiclient.discovery.build(
          API_SERVICE_NAME, API_VERSION, credentials=credentials)
      site_list = search_console_service.sites().list().execute()
    
      # Filter for verified URL-prefix websites.
      verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']
                            if s['permissionLevel'] != 'siteUnverifiedUser'
                            and s['siteUrl'].startswith('http')]
    
      # Print the sitemaps for all websites that you can access.
      results = '<!DOCTYPE html><html><body><table><tr><th>Verified site</th><th>Sitemaps</th></tr>'
      for site_url in verified_sites_urls:
    
        # Retrieve list of sitemaps submitted
        sitemaps = search_console_service.sitemaps().list(siteUrl=site_url).execute()
        results += '<tr><td>%s</td>' % (site_url)
    
        # Add a row with the site and the list of sitemaps
        if 'sitemap' in sitemaps:
          sitemap_list = "<br />".join([s['path'] for s in sitemaps['sitemap']])
        else:
          sitemap_list = "<i>None</i>"
        results += '<td>%s</td></tr>' % (sitemap_list)
    
      results += '</table></body></html>'
    
      # Save credentials back to session in case access token was refreshed.
      # ACTION ITEM: In a production app, you likely want to save these
      #              credentials in a persistent database instead.
      flask.session['credentials'] = credentials_to_dict(credentials)
    
      return results
    
    

  4. जांच के दौरान, हमें बैश एनवायरमेंट में OAUTHLIB_INSECURE_TRANSport को मैन्युअल तरीके से 1 पर सेट करना पड़ता था: export OAUTHLIB_INSECURE_TRANSPORT=1. अगर आपको सैंपल ऐप्लिकेशन चलाने के लिए ज़रूरी एचटीटीपीएस के बारे में गड़बड़ियां मिलती हैं, तो उस वैरिएबल को सेट करके देखें.