asp.net - 无需刷新 token 即可获取 AdWords customerId

标签 asp.net authentication google-ads-api

我正在开发一个允许用户连接 AdWords 帐户的网络应用程序。 1)因此,一旦用户连接了他们的 AW 帐户,我就会从 AW API 获取 token ,其中包括访问 token 和刷新 token 。到目前为止一切顺利,这只是一个典型的 OAuth2 流程。

2) 下次另一个用户连接同一个 AW 帐户时,AW 不会向我提供刷新 token ,假设我已将其存储在某处(这是预期的)。

所以问题来了,似乎没有办法在没有刷新 token 的情况下获取用户信息,这意味着我无法识别用户来检索刷新 token 。

我正在使用 .NET 库 (Google.Apis.Analytics.v3),它不允许我在不提供刷新 token 的情况下请求客户信息...

这里是示例代码:

var tokenResponse = await adWordsService.ExchangeCodeForTokenAsync(code, redirectUri);
var adWordsUser = adWordsService.GetAdWordsUser();
var customerService = (CustomerService)adWordsUser.GetService(AdWordsService.v201502.CustomerService);
var customer = customerService.get();

adWordsService 只是 API 的包装器。 因此,当我执行此行 var customer = customerService.get() 时,我收到以下错误:

ArgumentNullException: Value cannot be null.

Parameter name: AdWords API requires a developer token. If you don't have one, you can refer to the instructions at https://developers.google.com/adwords/api/docs/signingup to get one.

Google.Api.Ads.AdWords.Lib.AdWordsSoapClient.InitForCall(String methodName, Object[] parameters)

开发者 token 和所有客户端 ID 都在那里。

如果我在调用之前添加此行 adWordsUser.Config.AuthToken = tokenResponse.AccessToken;,它会提示刷新 token 。

ArgumentNullException: Value cannot be null.

Parameter name: Looks like your application is not configured to use OAuth2 properly. Required OAuth2 parameter RefreshToken is missing. You may run Common\Utils\OAuth2TokenGenerator.cs to generate a default OAuth2 configuration.

Google.Api.Ads.Common.Lib.OAuth2ProviderBase.ValidateOAuth2Parameter(String propertyName, String propertyValue)

所以问题是,如何在身份验证期间获取用户信息(在本例中为 customerId,根据本文 https://developers.google.com/adwords/api/docs/guides/optimizing-oauth2-requests#authenticating_multiple_accounts )以存储刷新 token ?

最佳答案

所以我刚刚发现问题出在 adWordsService 中。创建 AdWordsUser 时,不会为其分配包含最新身份验证 token 的更新身份验证提供程序。添加此身份验证提供程序后,AdWordsUser 不会尝试刷新 token ,因为 token 尚未过期,因此不需要刷新 token 。

关于asp.net - 无需刷新 token 即可获取 AdWords customerId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756778/

相关文章:

javascript - 授权 MVC3 ASP.NET

asp.net - Dot Net Nuke 中的垂直菜单栏

.net - 如何从 ASP.NET 检测 .NET 4?

asp.net-mvc - 是否可以使用 ASP.NET MVC 创建登录系统但不使用成员资格提供程序?

ruby-on-rails - 如何使用 devise_auth_token gem 获取与 rails-api 应用程序一起使用的 reset_password

google-analytics - Adwords 转换通过 React 引发了谷歌分析

asp.net - 为什么不允许通过查询更改列但很容易完成是服务器资源管理器

ios - 从 Facebook 登录获取真实姓名以解析用户

ruby-on-rails - 使用 Rails 跟踪转化时如何防止重复

Angular 6 谷歌广告词转换