การเริ่มต้นอย่างรวดเร็ว: เรียกใช้แอป Search Console ใน Python

เว็บแอปตัวอย่างนี้พิมพ์รายการเว็บไซต์ที่คุณเข้าถึงได้ และแผนผังเว็บไซต์ (หากมี) สำหรับเว็บไซต์เหล่านั้นแต่ละเว็บ

ข้อกำหนด

คุณต้องมีสิ่งต่อไปนี้จึงจะใช้โปรแกรมนี้ได้

  • เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์เพื่อให้สิทธิ์แอปตัวอย่าง
  • บัญชี Google ที่มีเว็บไซต์อย่างน้อย 1 เว็บไซต์ได้รับการยืนยันใน Google Search Console
  • เฟรมเวิร์กเว็บแอปพลิเคชัน Python 3 และ flask

วิธีการ

สำหรับตัวอย่างนี้ คุณจะปรับเปลี่ยน OAuth 2.0 สำหรับแอปพลิเคชันตัวอย่างสำหรับแอปพลิเคชันเว็บเซิร์ฟเวอร์ แอป Python ตัวอย่างนี้ใช้เฟรมเวิร์กเว็บแอปพลิเคชัน flask เพื่อเรียกใช้แอปพลิเคชันบนเว็บที่จัดการคีย์ OAuth และเรียกใช้ Google Cloud API คุณจะปรับตัวอย่างที่ลิงก์ไว้ให้เรียกใช้ Search Console API และพิมพ์ผลการค้นหาในหน้าเว็บ

ทำตามวิธีการตั้งค่าในหน้าตัวอย่าง OAuth ที่ลิงก์ด้านบน และคัดลอกโค้ดตัวอย่าง จากนั้นแก้ไขโค้ดตามที่แสดงด้านล่าง กล่าวคือ โปรดทำตามวิธีการตั้งค่าสำหรับสภาพแวดล้อมการเขียนโค้ด ตั้งค่า (หรือนำโปรเจ็กต์มาใช้ซ้ำ) ที่เข้าถึง Search Console API ในคอนโซล Google Cloud ได้ และสร้างข้อมูลเข้าสู่ระบบสำหรับเว็บแอปพลิเคชัน

คุณจะใช้ตัวอย่างโค้ดที่สมบูรณ์ สำหรับ Python เป็นซอร์สโค้ดสำหรับตัวอย่างนี้

อ่านการแก้ไขด้านล่างเพื่อดูการเปลี่ยนแปลงที่คุณต้องทำในวิธีการที่ลิงก์ไว้

หากต้องการเข้าถึงไคลเอ็นต์ Python ของ Google API จากโปรเจ็กต์ Google App Engine คุณจะต้องใช้บัญชีบริการในการจัดการสิทธิ์

การปรับเปลี่ยน

เมื่อทำตามวิธีการในหน้าตัวอย่าง Oauth2 ที่ลิงก์ ให้ทำการเปลี่ยนแปลงต่อไปนี้

  1. เปิดใช้ Google Search Console API แทน Drive 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 ในสภาพแวดล้อม Bash: export OAUTHLIB_INSECURE_TRANSPORT=1 หากคุณได้รับข้อผิดพลาดเกี่ยวกับ HTTPS ที่ต้องใช้เพื่อเรียกใช้แอปตัวอย่าง ให้ลองตั้งค่าตัวแปรดังกล่าว