我通过将GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为我的App Engine服务帐户(例如example@appspot.gserviceaccount.com
)密钥的路径进行身份验证。
如果日历直接与App Engine服务帐户共享,则可以执行以下操作:
let googleCalendar = google.calendar({
version: 'v3',
auth: new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar'],
})
});
但是我希望用户与我拥有的另一个服务帐户google-calendar@example.iam.gserviceaccount.com
共享日历。因此,我希望App Engine服务帐户可以模拟google-calendar
帐户。我已经尝试过了(以及其他一些小的变化):
let googleCalendar = google.calendar({
version: 'v3',
auth: new google.auth.GoogleAuth({
clientOptions: {
subject: 'google-calendar@uniserval-app.iam.gserviceaccount.com'
},
scopes: ['https://www.googleapis.com/auth/calendar'],
})
});
我收到401错误:Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.
我尝试使App Engine服务帐户成为google-calendar
服务帐户的成员,并授予其各种角色,例如Service Account User
和Service Account Token Creator
-但没有任何变化。
我怀疑代码是正确的,但是我没有正确配置角色...但是到目前为止,我一直在搜索如何执行此操作两天,但我找不到任何有关如何正确执行操作的文档为此。
答案 0 :(得分:0)
您的语法看起来正确,只是您似乎误解了将哪个电子邮件地址设置为主题。主题是您希望代理的用户帐户。
new google.auth.GoogleAuth({
clientOptions: {
subject: 'someone@yourdomain.com'
},
scopes: ['https://www.googleapis.com/auth/calendar'],
})