การเริ่มต้นอย่างรวดเร็ว: เรียกใช้แอป Search Console ใน Python
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แอปพลิเคชันเว็บตัวอย่างนี้จะพิมพ์รายการไซต์ที่คุณสามารถเข้าถึงได้ และ Sitemap
หากมี สำหรับแต่ละเว็บไซต์
ข้อกำหนด
คุณต้องมีสิ่งต่อไปนี้จึงจะใช้โปรแกรมนี้ได้
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์เพื่อให้สิทธิ์แอปตัวอย่าง
- บัญชี Google ที่มีเว็บไซต์อย่างน้อย 1 เว็บไซต์ได้รับการยืนยันใน Google Search Console
- เฟรมเวิร์กเว็บแอปพลิเคชัน Python 3 และ flask
วิธีการ
สำหรับตัวอย่างนี้ คุณจะปรับเปลี่ยน OAuth 2.0 สำหรับตัวอย่างแอปพลิเคชันเว็บเซิร์ฟเวอร์
แอปพลิเคชัน
แอป Python ตัวอย่างนี้ใช้เฟรมเวิร์กเว็บแอปพลิเคชันแบบขวดแก้วทดลองเพื่อเรียกใช้
แอปพลิเคชันบนเว็บที่จัดการคีย์ OAuth และเรียก Google Cloud API คุณ
จะปรับตัวอย่างที่ลิงก์ไว้ให้เรียก Search Console API และพิมพ์
ผลลัพธ์ในหน้าเว็บ
ปฏิบัติตามคำแนะนำการตั้งค่าในหน้าตัวอย่างของ OAuth ที่ลิงก์ด้านบน และคัดลอก
โค้ดตัวอย่าง จากนั้นให้แก้ไขโค้ดดังที่แสดงด้านล่าง โปรดทำตาม
วิธีการตั้งค่าสภาพแวดล้อมการเขียนโค้ด การตั้งค่า (หรือการใช้ซ้ำ) โปรเจ็กต์
ที่เข้าถึง Search Console API ใน Google Cloud Console ได้และการสร้างข้อมูลเข้าสู่ระบบ
สำหรับเว็บแอปพลิเคชัน
คุณจะใช้ตัวอย่างโค้ดที่สมบูรณ์ สำหรับ
Python เป็นซอร์สโค้ดของตัวอย่างนี้
อ่านการแก้ไขด้านล่างเพื่อดูการเปลี่ยนแปลงที่คุณต้องทำในลิงก์
วิธีทำ
หากคุณต้องการเข้าถึงไคลเอ็นต์ Python ของ Google API จาก Google App Engine
คุณจะต้องใช้บัญชีบริการ
เพื่อจัดการสิทธิ์
การปรับเปลี่ยน
เมื่อทำตามวิธีการในหน้าตัวอย่างของ Oauth2 ที่ลิงก์ไว้ ให้ตั้ง
การเปลี่ยนแปลงต่อไปนี้:
- เปิดใช้ Google Search Console API แทน Drive API
คัดลอกแอปพลิเคชันตัวอย่างจากท้ายเอกสาร 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'
แทนที่เนื้อหาของภาษา 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
ในการทดสอบ เราจำเป็นต้องตั้งค่า OAUTHLIB_INSECURE_TRANSPORT ด้วยตนเอง
เป็น 1 ในสภาพแวดล้อม Bash: export OAUTHLIB_INSECURE_TRANSPORT=1
หากคุณได้รับข้อผิดพลาดเกี่ยวกับ HTTPS ที่ต้องใช้เพื่อเรียกใช้แอปตัวอย่าง ให้ลองตั้งค่า
ตัวแปร
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[[["\u003cp\u003eThis web application displays a list of your verified websites in Google Search Console and their corresponding sitemaps.\u003c/p\u003e\n"],["\u003cp\u003eTo run the application, you need internet access, a Google account with a verified website in Search Console, and Python 3 with the Flask framework installed.\u003c/p\u003e\n"],["\u003cp\u003eThe application utilizes the OAuth 2.0 protocol for authorization and the Google Search Console API to retrieve website and sitemap data.\u003c/p\u003e\n"],["\u003cp\u003eYou will need to modify a provided OAuth 2.0 sample application by enabling the Search Console API and adjusting scopes, service name, and API version.\u003c/p\u003e\n"],["\u003cp\u003eThe application code is modified to retrieve and display verified site URLs and associated sitemap information in a tabular format within a web page.\u003c/p\u003e\n"]]],["This Python web app uses Flask and the Google Search Console API to display a list of verified websites associated with a Google account. It requires internet access, a verified website in Google Search Console, and specific Python libraries. The app utilizes OAuth 2.0 for authorization, and the core action is adapting the linked OAuth sample code to call the Search Console API. The modified code retrieves and displays verified site URLs and their associated sitemaps in a web page table, by listing site and then the sitemaps list.\n"],null,["# Quickstart: Run a Search Console App in Python\n\nThis sample web app prints the list of sites that you can access, and the sitemaps,\nif any, for each of those sites.\n\nRequirements\n------------\n\nTo run this program, you'll need:\n\n- Access to the internet and a web browser, in order to authorize the sample app.\n- A Google account with at least one website [verified](https://support.google.com/webmasters/answer/9008080) in Google Search Console.\n- Python 3 and the [flask](https://github.com/pallets/flask) web application framework.\n\nInstructions\n------------\n\nFor this sample, you'll adapt the [OAuth 2.0 for Web Server Applications sample\napplication](https://developers.google.com/identity/protocols/oauth2/web-server#python).\nThis sample python app uses the flask web application framework to run a\nweb-based application that manages OAuth keys and calls a Google Cloud API. You\nwill adapt the linked sample to call the Search Console API and print out the\nresults in a web page.\n\nFollow the setup instructions on the OAuth sample page linked above, and copy the\nsample code, then modify the code as shown below. Specifically, follow the\nsetup instructions for your coding environment, setting up (or reusing) a project\nthat can access the Search Console API in the Google Cloud console, and generating credentials\nfor a web application.\n\nYou will use the [Complete code example](https://developers.google.com/identity/protocols/oauth2/web-server#example) **for\nPython** as the source code for this sample.\n\nRead *Modifications* below to see what changes you need to make to the linked\ninstructions.\n\nIf you need to access the Google API Python Client from a Google App Engine\nproject, you'll need to use a [service account](https://developers.google.com/identity/protocols/oauth2/service-account)\nto manage your permissions.\n\nModifications\n-------------\n\nWhen following the instructions on the linked Oauth2 sample page, make the\nfollowing changes:\n\n1. Enable the Google Search Console API rather than the Drive API.\n2. Copy the sample application at the end of the OAUth document linked above,\n and replace this\n\n ```\n SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']\n API_SERVICE_NAME = 'drive'\n API_VERSION = 'v2'\n ```\n With this: \n\n ```\n SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']\n API_SERVICE_NAME = 'searchconsole'\n API_VERSION = 'v1'\n ```\n\n \u003cbr /\u003e\n\n3. Replace the body of the Python language\n [`test_api_request()`](https://developers.google.com/identity/protocols/oauth2/web-server#example)\n function with the following code:\n\n ```python\n @app.route('/test')\n def test_api_request():\n if 'credentials' not in flask.session:\n return flask.redirect('authorize')\n\n # Load credentials from the session.\n credentials = google.oauth2.credentials.Credentials(\n **flask.session['credentials'])\n\n # Retrieve list of properties in account\n search_console_service = googleapiclient.discovery.build(\n API_SERVICE_NAME, API_VERSION, credentials=credentials)\n site_list = search_console_service.sites().list().execute()\n\n # Filter for verified URL-prefix websites.\n verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']\n if s['permissionLevel'] != 'siteUnverifiedUser'\n and s['siteUrl'].startswith('http')]\n\n # Print the sitemaps for all websites that you can access.\n results = '\u003c!DOCTYPE html\u003e\u003chtml\u003e\u003cbody\u003e\u003ctable\u003e\u003ctr\u003e\u003cth\u003eVerified site\u003c/th\u003e\u003cth\u003eSitemaps\u003c/th\u003e\u003c/tr\u003e'\n for site_url in verified_sites_urls:\n\n # Retrieve list of sitemaps submitted\n sitemaps = search_console_service.sitemaps().list(siteUrl=site_url).execute()\n results += '\u003ctr\u003e\u003ctd\u003e%s\u003c/td\u003e' % (site_url)\n\n # Add a row with the site and the list of sitemaps\n if 'sitemap' in sitemaps:\n sitemap_list = \"\u003cbr /\u003e\".join([s['path'] for s in sitemaps['sitemap']])\n else:\n sitemap_list = \"\u003ci\u003eNone\u003c/i\u003e\"\n results += '\u003ctd\u003e%s\u003c/td\u003e\u003c/tr\u003e' % (sitemap_list)\n\n results += '\u003c/table\u003e\u003c/body\u003e\u003c/html\u003e'\n\n # Save credentials back to session in case access token was refreshed.\n # ACTION ITEM: In a production app, you likely want to save these\n # credentials in a persistent database instead.\n flask.session['credentials'] = credentials_to_dict(credentials)\n\n return results\n ```\n\n \u003cbr /\u003e\n\n4. In our testing, we needed to manually set OAUTHLIB_INSECURE_TRANSPORT\n to 1 in the Bash environment: `export OAUTHLIB_INSECURE_TRANSPORT=1`.\n If you get errors about HTTPS required to run the sample app, try setting that\n variable."]]