如何更改 google API "service account" name/email 地址

How to change the google API "service account" name/email address

我正在使用 Google API 在 google 驱动器上创建电子表格。

一切正常。

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

这可能吗? -

您不能更改服务帐户电子邮件地址,也不能为其提供真实世界的用户名。 service account email address is created by Google in the Google Developer console

更改电子邮件地址的唯一方法是将其删除并创建一个新地址,但您仍然会被 Google 创建的地址困住。我怀疑客户端 ID 和电子邮件地址是一对用于识别您的应用程序的。类似于 client id 和 client secret,但我无法验证。

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

Google 服务帐户允许您模拟现有的用户帐户(对于某些服务)。我没有在 Google 驱动器上测试过它,但我已经将它与网站管理员工具 API 一起使用过。说明可在此处找到:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

总而言之,在创建服务帐户凭据时,您可以指定 "sub" 参数和 "The email address of the user for which the application is requesting delegated access." 您请求访问的帐户必须存在并且有权访问服务你在请求

在上面的link中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-客户端 gem (0.9.pre3) 并且 ServiceAccountCredentials 构造函数没有传递 'sub' 参数传递给它的父 class 所以它必须在另一个步骤中指定:

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 

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