加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中使用AccessType = Offline的Google Analytics OAuth

发布时间:2020-12-15 19:46:52 所属栏目:百科 来源:网络整理
导读:我想使用OAuth使用Google AnalyticsAPI. 我正在使用这个库: http://code.google.com/p/google-api-dotnet-client/ 以下代码用于身份验证: var credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "...",ClientSec
我想使用OAuth使用Google AnalyticsAPI.

我正在使用这个库:
http://code.google.com/p/google-api-dotnet-client/

以下代码用于身份验证:

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets { ClientId = "...",ClientSecret = "..." },new[] {Google.Apis.Analytics.v3.AnalyticsService.Scope.AnalyticsReadonly},"user",CancellationToken.None,new FileDataStore("Analytics.Auth.Store")).Result;

var service = new Google.Apis.Analytics.v3.AnalyticsService(
    new BaseClientService.Initializer
    {
        HttpClientInitializer = credential,ApplicationName = "...",});

我可以将它与refresh_token一起使用,这样我就不必每隔几天就接受一次授权请求吗?

类似于这个问题的答案:
Service Account Google Analytics OAuth AccessType = Offline C#

解决方法

我只知道一种方法:你需要覆盖GoogleAuthorizationCodeRequestUrl,但我不知道如何在AuthorizationBroker中使用它.

internal class ForceOfflineGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
    public ForceOfflineGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer) : base(initializer) { }

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
    {
        return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
                {
                    ClientId = ClientSecrets.ClientId,Scope = string.Join(" ",Scopes),RedirectUri = redirectUri,AccessType = "offline",ApprovalPrompt = "force"
                };
    }
};

看起来他们在经纪人中创建Flow:
GoogleWebAuthorizationBroker.cs

我没有看到任何方式传递params或替换AuthorizationCodeFlow

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读