【发布时间】:2020-10-01 14:54:26
【问题描述】:
我有一个 Appscript 应用程序,它使用 Google's OAuth2 library 在 G Suite 域上执行各种功能。我想添加使用Transfer API 传输用户数据的功能,但我在授权服务时遇到了问题。我对 gmail、驱动器和目录 API 的授权没有任何问题,并且我确保该服务在 GCP 项目中处于启用状态。但是,我无法让我的 OAuth2 服务获得授权,我每次都会收到我的服务帐户未获得授权的记录消息。这里缺少什么?
这是我的代码:
const migrationScope = ['https://www.googleapis.com/auth/admin.datatransfer'];
function migrateUser(sourceUser, targetUser, type) {
var url = 'https://www.googleapis.com/admin/datatransfer/v1/transfers';
var service = getService_('superadmin_account@company.com', 'admin', migrationScope);
var transferObject = {
oldOwnerUSerId: getUserId(sourceUser), // this takes an email and returns the user ID which is required by the API
newOwnerUserId: getUserId(targetUser),
applicationDataTransfers: [
{
applicationTransferParams: [
{
value: type,
}
],
applicationId: 5********6 // Our App ID for Drive
}
]
}
if (service.hasAccess()) {
var response = UrlFetchApp.fetch(url, requestBuilder(service, transferObject, 'POST'));
try {
var result = JSON.parse(response.getContentText());
return (result);
}
catch (e) {
return (undefined);
}
} else { Logger.log("Service does not have access to migrate content."); }
}
function getService_(email, service, scopes) {
return OAuth2.createService(service + ':' + email)
.setTokenUrl('https://oauth2.googleapis.com/token').
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
.setSubject(email)
.setPropertyStore(PropertiesService.getScriptProperties())
.setScope(scopes);
}
function requestBuilder(service, payload, method) {
return {
method: method,
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
}
【问题讨论】:
标签: google-apps-script google-admin-sdk