【问题标题】:Google analytics C# project - authentication fails谷歌分析 C# 项目 - 身份验证失败
【发布时间】:2017-04-03 14:15:55
【问题描述】:

我正在使用这个简单的服务器端代码创建一个 google 身份验证令牌,用于在客户端嵌入视图

    private void GenarateGAToken()
    {
        GoogleCredential credential;
        string keyFilePath = Server.MapPath("") + MYKEYFILENAME;
        using (Stream stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            credential = GoogleCredential.FromStream(stream);
        }
        string[] scopes = new string[] { AnalyticsService.Scope.Analytics };             credential = credential.CreateScoped(scopes);

        try
        {
            bearer = ((ITokenAccess)credential).GetAccessTokenForRequestAsync().Result;
        }
        catch (AggregateException ex)
        {
            throw ex.InnerException;
        }
    }

我使用页面上的token来授权API

gapi.analytics.ready(function() {
    gapi.analytics.auth.authorize({
    'serverAuth': {
        'access_token': '{{ <%=bearer%> }}'
    }
    });

但我收到错误 401“无效凭据”:

{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= 上测试了令牌值 它看起来有效

Google 帐户似乎已正确设置为具有所有必需权限的服务帐户

我在这里错过了什么?

【问题讨论】:

    标签: c# google-analytics


    【解决方案1】:

    好吧,发现了愚蠢的复制粘贴问题 我复制并粘贴了这个:

    gapi.analytics.ready(function() {
        gapi.analytics.auth.authorize({
        'serverAuth': {
            'access_token': '{{ <%=bearer%> }}'
        }
        });
    

    大括号是问题所在,它们只是占位符,应该被删除,所以这可行:

    gapi.analytics.ready(function() {
        gapi.analytics.auth.authorize({
        'serverAuth': {
            'access_token': '<%=bearer%>'
        }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-26
      • 2016-11-11
      • 2018-07-02
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多