我正在嘗試通過其 API 從 Google Search Console 恢復 Google Discover 資料并將其顯示在 Google Sheet 中。
我正在學習一個教程,該教程引導我在 Google Cloud 平臺中設定所需的身份驗證,并向我展示了如何設定一個基本請求來檢索我在 Search Console 中可以訪問的網站串列。
我不明白的是如何修改下面的代碼,以便 API 檢索 Google Discover 資料而不是我可以訪問的網站串列。
Google Apps 腳本代碼獲取提到的網站并填充兩列,一列包含網站鏈接,另一列包含我對該網站的訪問級別。
function listAccountSites() {
var service = getService();
if (service.hasAccess()) {
var apiURL = "https://www.googleapis.com/webmasters/v3/sites";
var headers = {
"Authorization": "Bearer " getService().getAccessToken()
};
var options = {
"headers": headers,
"method" : "GET",
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(apiURL, options);
var json = JSON.parse(response.getContentText());
Logger.log(json)
var URLs = []
for (var i in json.siteEntry) {
URLs.push([json.siteEntry[i].siteUrl, json.siteEntry[i].permissionLevel]);
}
s_sites.getRange(2,1,URLs.length,2).setValues(URLs);
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
Browser.msgBox('Open the following URL and re-run the script: ' authorizationUrl)
}
}
Using this Google documentation page I was able to construct an API request that would get the Google Discover data I need, but as I said I cannot figure out how to do this. The mentioned request gets the data I need is the following one:
{
"startDate": "2021-11-01",
"endDate": "2021-11-05",
"dimensions": [
"PAGE"
],
"type": "DISCOVER"
}
uj5u.com熱心網友回復:
我相信你的目標如下。
您希望使用 Google Apps 腳本實作以下請求。參考
POST https://www.googleapis.com/webmasters/v3/sites/https://www.example.com//searchAnalytics/query?key={MY_API_KEY} { "startDate": "2015-04-01", "endDate": "2015-05-01", "dimensions": ["country","device"] }在您的情況下,您希望使用以下請求正文。
{ "startDate": "2021-11-01", "endDate": "2021-11-05", "dimensions": [ "PAGE" ], "type": "DISCOVER" }
如果我的理解是正確的,下面的示例腳本怎么樣?
示例腳本:
此示例腳本假設您的訪問令牌可以使用并且您已經能夠使用該 API。請注意這一點。
var siteUrl = "https://www.example.com/"; // Please set the site URL.
var apiURL = `https://www.googleapis.com/webmasters/v3/sites/${encodeURIComponent(siteUrl)}/searchAnalytics/query`;
var headers = { "Authorization": "Bearer " getService().getAccessToken() };
var payload = {
"startDate": "2021-11-01",
"endDate": "2021-11-05",
"dimensions": ["PAGE"],
"type": "DISCOVER"
};
var options = {
"headers": headers,
"method": "POST",
"muteHttpExceptions": true,
"contentType": "application/json",
"payload": JSON.stringify(payload),
};
var response = UrlFetchApp.fetch(apiURL, options);
參考:
- 搜索分析:查詢
- 獲取(網址,引數)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/358794.html
標籤:google-apps-script google-sheets google-api google-search-console
