我正在尝试使用此API 管理我工作区中的公司设备。第一步是拉出当前设备的列表。代码在应用程序脚本中。
我越来越:
Exception: Request failed for https://cloudidentity.googleapis.com returned code 403. Truncated server response: {
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}
}
我做过的事情
- 服务帐户对 cloud-platform、cloud-identity 和 cloud-identity.devices 具有域范围的委派
- 身份验证是通过 Google 的 OAuth 库完成的,似乎工作正常。
- OAuth 也有范围的空间,需要相同的空间。
代码位:
var scriptProperties = PropertiesService.getScriptProperties()
private_key= scriptProperties.getProperty('private_key').replace(/\\n/g, '\n')
client_email = scriptProperties.getProperty("client_email")
;
function getOAuthService() {
return OAuth2.createService('Service Account')
// Set the endpoint URLs
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the client ID and secret
.setPrivateKey(private_key)
.setIssuer(client_email)
// Set the property store where authorized tokens should be persisted
.setPropertyStore(PropertiesService.getScriptProperties())
// .setCache(CacheService.getUserCache())
.setParam('access_type', 'offline')
.setScope('https://www.googleapis.com/auth/cloud-identity');
}
function reset() {
var service = getOAuthService();
service.reset();
}
function downloadDevices() {
var service = getOAuthService();
service.reset();
if (service.hasAccess()) {
var pageToken;
var URL = "https://cloudidentity.googleapis.com/v1/devices";
var headers = {
Authorization: 'Bearer ' + service.getAccessToken()
};
var options = {
method : "GET",
headers: headers
};
var response = UrlFetchApp.fetch(URL, options);
} else {
Logger.log('service Error');
Logger.log(service.getLastError());
}
}
看起来一切都很好,只是我没有权限。但据我所知,没有更多相关的权限可以给予。