如何更改 google API“服务帐户”名称/电子邮件地址

Posted

技术标签:

【中文标题】如何更改 google API“服务帐户”名称/电子邮件地址【英文标题】:How to change the google API "service account" name/email address 【发布时间】:2015-07-08 02:14:02 【问题描述】:

我正在使用 Google API 在 Google Drive 上创建电子表格。

我的应用有一个链接显示“下载到 Google Doc” 应用将文件发送到 google docs 并与用户共享。 然后用户可以在 google 文档中看到它。

这一切都很好。

问题在于共享文档的服务帐户的身份。 它是一个生成的 id。我希望它被标记到我的应用程序中。

这可能吗? -

【问题讨论】:

您对 API 的使用是否与 google 帐户中的项目相关联?那么id和项目之间有什么共同点吗? 您无法更改,服务帐户电子邮件地址是由 Google 创建的,您无法使用它。不,您也不能提供自己的,它必须是由 Google 生成的。 @DaImTo 将该评论作为答案,我会寄给你一张支票! 【参考方案1】:

您不能更改服务帐户电子邮件地址,也不能为其提供真实的用户名。 service account 电子邮件地址由 Google 在 Google Developer console 中创建。

更改电子邮件地址的唯一方法是删除它并创建一个新地址,但您又会被 Google 创建的那个卡住。我怀疑客户 ID 和电子邮件地址是用于识别您的应用程序的一对。类似于客户端 ID 和客户端密码,但我无法验证。

我确实明白你的意思,如果我们可以,那就太好了。

【讨论】:

【参考方案2】:

Google 服务帐户确实允许您模拟现有用户帐户(对于某些服务)。我没有在 Google Drive 上测试过它,但我已经将它与 Webmaster tools API 一起使用。说明可以在这里找到:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

总而言之,在创建服务帐户凭据时,您可以使用“应用程序为其请求委托访问的用户的电子邮件地址”指定“sub”参数。您请求访问的帐户必须存在并且有权访问您请求的服务。

在上面的链接中,Google 提供了 Java、Python 和 HTTP/REST 的示例,以下是 Python 示例:

credentials = SignedJwtAssertionCredentials(client_email, private_key,
'https://www.googleapis.com/auth/sqlservice.admin',
sub='user@example.org')

我正在使用 Ruby google-api-client gem (0.9.pre3) 并且 ServiceAccountCredentials 构造函数没有将“sub”参数传递给其父类,因此必须在另一个步骤中指定它:

client = Google::Auth::ServiceAccountCredentials.new(json_key_io: json_key_io, scope: scope)
client.update!(sub: 'user@example.org')
client.fetch_access_token!
service = Google::Apis::WebmastersV3::WebmastersService.new
service.authorization = client 

使用此代码,我可以使用我的服务帐户进行身份验证,并且我可以访问网站

【讨论】:

我跟着这个并得到一个错误:google.auth.exceptions.RefreshError: ('invalid_grant: Invalid email or User ID', '\n "error": "invalid_grant",\n "error_description": "Invalid email or User ID"\n')

以上是关于如何更改 google API“服务帐户”名称/电子邮件地址的主要内容,如果未能解决你的问题,请参考以下文章

如何使用服务帐户访问 Google Doc Api?

如何更改 Google Compute Engine 服务帐户的范围以将数据写入 Google Cloud Storage 存储桶?

如何在 Twilio 无服务器功能中将 googleapis google.auth.GoogleAuth() 用于 google API 服务帐户?

如何在 C# 中使用服务帐户登录 Google API - 凭据无效

如何通过 Google Drive .NET API v3 使用服务帐户访问 Team Drive

如何使用API Explorer作为服务帐户?