简体   繁体   English

带有服务帐户问题的Google Contacts API

[英]Google Contacts API with Service Account issue

So, I've basically got this working, except for one issue. 因此,除了一个问题之外,我基本上已经可以完成这项工作。 I've got a google service account set up so it can access our domain contacts. 我已经设置了一个Google服务帐户,以便可以访问我们的域联系人。 And it can batch query them perfectly! 它可以批量查询它们!

But if I call cr.Retrieve("some-contact-url-here"), it throws an error griping about not having a Refresh token. 但是,如果我调用cr.Retrieve(“ some-contact-url-here”),则会引发一个错误,提示您没有刷新令牌。 I'm using a service account though, so I don't get a refresh token when I authenticate. 我使用的是服务帐户,因此进行身份验证时没有刷新令牌。

And I can't seem to find any good answer as to how I'm supposed to get a refresh token for a service account. 关于如何为服务帐户获取刷新令牌,我似乎找不到任何好的答案。 There's one or two stackoverflow posts which actively mention getting a refresh token with a service account....but what they linked to has since been redirected. 有一两个stackoverflow帖子主动提到使用服务帐户获取刷新令牌....但是他们链接到的内容自此以后被重定向。 Anything else I've found to do with refresh tokens has basically been about authenticating manually and storing the token. 我发现与刷新令牌有关的所有其他事情基本上都是关于手动验证和存储令牌的。 Because I need to use a Service Account, that is not a possibility. 因为我需要使用服务帐户,所以这是不可能的。

A service account's credentials, which you obtain from the Google Developers Console, include a generated email address that is unique, a client ID, and at least one public/private key pair. 您从Google Developers Console获得的服务帐户的凭据包括生成的唯一电子邮件地址,客户端ID和至少一个公共/专用密钥对。 You use the client ID and one private key to create a signed JWT and construct an access-token request in the appropriate format. 您可以使用客户端ID和一个私钥来创建签名的JWT,并以适当的格式构造访问令牌请求。 Your application then sends the token request to the Google OAuth 2.0 Authorization Server, which returns an access token. 然后,您的应用程序将令牌请求发送到Google OAuth 2.0授权服务器,该服务器会返回访问令牌。 The application uses the token to access a Google API. 该应用程序使用令牌访问Google API。 When the token expires, the application repeats the process. 当令牌过期时,应用程序将重复该过程。

Check this page for more information. 检查此页面以获取更多信息。

Ok, based on several hours of bashing my head violently against the API, it looks like there's basically no way to get a refresh token when you're authenticating as a Service Account. 好的,基于对API猛烈抨击几个小时后,当您作为服务帐户进行身份验证时,似乎基本上没有办法获得刷新令牌。 Expected behavior, really. 确实是预期的行为。

Anyway, to get around the issue, you can load all of the contacts into memory, 无论如何,要解决此问题,您可以将所有联系人加载到内存中,

feed.AutoPaging = true;
foreach (var c in feed.Entries)
{
    contactList.Add(c);
}

And you can update|delete|etc then. 然后,您可以更新|删除|等。 Grossly inefficient though. 虽然效率很低。 Especially if your contact list gets rather big. 特别是如果您的联系人列表很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM