Google API不会在测试服务器上授权网站,而是在localhost上工作

问题描述 投票:0回答:1

我的网站使用谷歌驱动器API只会影响我们的测试服务器,而不是我测试它的localhost。你可以想象这使得很难排除故障。在程序运行后写入文本文件后,我设法找到导致问题的确切语句:

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

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

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

44354以上是我的本地主机,黑屏的http(不是https)网址是我们的测试服务器。你可以看到原点和重定向uris是完全一样的,所以我无法弄清楚它为什么会在一个而不是另一个上工作。

这是语句流来自:

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

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

c# asp.net-mvc google-api google-oauth google-api-dotnet-client
1个回答
0
投票

我最终通过大量的反复试验弄明白了。

我不得不添加这段代码:

            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
        };

(我不认为tokentype或expires是重要的,但无论如何我包括它们)

最后:

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

然后,该凭证的使用方式与之前相同。

© www.soinside.com 2019 - 2024. All rights reserved.