我正在尝试在 Google Script 中使用 doubleclick-search (SearchAds360) api。
我已经使用另一个应用程序生成了一个刷新令牌,并且有一个工作流可以将刷新令牌交换为谷歌脚本中的访问令牌,没有问题。
当我尝试使用访问令牌作为授权标头来请求报告时,我得到的响应是:
"domain": "global",
"reason": "conditionNotMet",
"message": "Permission denied: the DoubleClick Search user does not have read access to the report scope.",
"locationType": "header",
"location": "If-Match"
如果我记录访问令牌,然后将其放回用于获取刷新令牌的应用程序中,我可以毫无问题地使用它来请求报告,但 Google Script 拒绝工作。
我已手动将身份验证范围添加到 GS 清单文件中,该文件现在显示:
4 OAuth Scopes required by the script:
https://www.googleapis.com/auth/doubleclicksearch
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/spreadsheets.readonly
https://www.googleapis.com/auth/userinfo.email
我使用的代码 - 可能不完整
var report_cook_url = "https://www.googleapis.com/doubleclicksearch/v2/reports?fields=id,isReportReady,files"
var tokenUrl = 'https://accounts.google.com/o/oauth2/token';
function getAccessToken(){
var headers = {'host':'www.googleapis.com'};
var payload = {'client_id':client_id,
'client_secret': client_secret,
'refresh_token': refresh_token,
'grant_type':'refresh_token',
'scope':'https://www.googleapis.com/auth/doubleclicksearch https://www.googleapis.com/auth/userinfo.email openid'
};
var params = {
'method':'post',
'payload':payload
};
return UrlFetchApp.fetch(tokenUrl, params);
}
function bulkCookReport(){
var token_response = getAccessToken();
if(token_response.getResponseCode() != 200){
return;
}
var access_token = JSON.parse(token_response.getContentText()).access_token;
var headers = {"Authorization": 'Bearer '+ access_token,
"Content-Type": 'application/json'};
Logger.log(access_token);
var requests = [];
//var reports = reports_to_run();
//var report_names = Object.keys(reports);
var aReport, aRequest
aRequest = newRequestObj();
aRequest.headers = {};
aRequest.headers.Authorization = "Bearer "+ access_token;
aRequest.payload = JSON.stringify(aReport);
requests.push(aRequest);
//var report_ids = UrlFetchApp.fetchAll(requests);
var response = UrlFetchApp.fetch(report_cook_url, aRequest);
//Logger.log(report_ids[0].getContentText());
Logger.log(response.getContentText());
//Logger.log(response);
}
function newRequestObj(){
return {'url': report_cook_url,
'method':'post',
'muteHttpExceptions':true,
'contentType':'application/json'
}
}