【问题标题】:Google api not authorizing for site on test server, but working on localhostGoogle api未授权测试服务器上的站点,但在本地主机上工作
【发布时间】:2017-12-18 22:29:43
【问题描述】:

我的网站使用谷歌驱动 API 时遇到问题,该 API 仅影响我们拥有它的测试服务器,但不影响我正在测试它的本地主机。正如您可以想象的那样,这使得故障排除变得困难。在程序运行时写入文本文件后,我设法找到了导致问题的确切语句:

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                null).Result;

这让我相信该错误与权限/授权有关。我的本地主机和测试站点权限在 google api 控制台上完全相同,这就是我从中获取客户端机密的地方。

https://i.imgur.com/fJDAWWz.png

44354 以上是我的本地主机,涂黑的 http(不是 https)url 是我们的测试服务器。 如您所见,来源和重定向 uri 完全相同,所以我无法弄清楚为什么它会在一个而不是另一个上起作用。

这是语句流来自:

var stream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/client_secret.json"), FileMode.Open, FileAccess.Read)

编辑:我刚刚意识到测试服务器实际上是 http 地址,而不是 https 地址。这可能是为什么? google API 调用不能通过 http 工作吗?

【问题讨论】:

标签: c# asp.net-mvc google-api google-oauth google-api-dotnet-client


【解决方案1】:

经过多次反复试验,我最终弄明白了。

我不得不添加这段代码:

            GoogleClientSecrets cs = GoogleClientSecrets.Load(stream);

            flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = cs.Secrets.ClientId,
                    ClientSecret = cs.Secrets.ClientSecret
                },
                Scopes = Scopes,
            });

然后得到一个令牌。为了获得令牌,我按照此处的说明操作,在 OAuth Playground 下:https://developers.google.com/adwords/api/docs/guides/authentication

然后使用那里的信息来制作:

        TokenResponse token = new TokenResponse
        {
            RefreshToken = "refreshtokenhere",
            TokenType = "Bearer",
            ExpiresInSeconds = 3600
        };

(我不认为令牌类型或过期时间很重要,但我还是把它们包括在内)

最后:

credential = new UserCredential(flow, "me", token);

然后以与以前相同的方式使用此凭据。

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2013-01-14
    • 2021-12-13
    • 2020-12-25
    • 2018-03-07
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多