การเริ่มต้นอย่างรวดเร็ว: เรียกใช้แอป 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 เป็นซอร์สโค้ดสำหรับตัวอย่างนี้

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

หากต้องการเข้าถึงไคลเอ็นต์ Google API Python จากโปรเจ็กต์ 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 ที่ต้องใช้ในการเรียกใช้แอปตัวอย่าง ให้ลองตั้งค่าตัวแปรนั้น