简体   繁体   English

Google API未授权测试服务器上的站点,但在localhost上工作

[英]Google api not authorizing for site on test server, but working on localhost

I'm having a problem with my site that uses google drive API that only affects the test server we have it on, but not localhost where I am testing it. 我的网站使用Google Drive API时出现问题,该问题仅会影响我们拥有它的测试服务器,而不会影响我正在对其进行测试的localhost。 As you can imagine this makes it hard to troubleshoot. 如您所料,这使故障排除变得很困难。 After writing to a text file as the program runs I managed to find the exact statement causing the issues: 在程序运行时写入文本文件后,我设法找到了导致问题的确切语句:

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

Which leads me to believe the error has something to do with permissions/authorization. 这使我相信该错误与权限/授权有关。 My localhost and test site permissions are exactly the same on the google api console, and this is where I get my client secrets from. 我的localhost和测试站点权限在google api控制台上完全相同,这是我从中获取客户端机密的地方。

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

Above 44354 is my localhost and the blacked out http (not https) url is our test server. 高于44354的是我的本地主机,涂黑的http(不是https)url是我们的测试服务器。 As you can see the origins and redirect uris are exactly the same, so I can't figure out why it would work on one and not the other. 如您所见,起源和重定向uri完全相同,所以我不知道为什么它可以在一个而不是另一个上工作。

This is the statement stream comes from: 这是语句流来自:

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

EDIT: I just realized the test server is actually the http address, not the https address. 编辑:我只是意识到测试服务器实际上是http地址,而不是https地址。 Could this be why? 这可能是为什么吗? Do google API calls not work over http? Google API调用不能通过http工作吗?

I eventually figured it out through much trial and error. 我最终通过反复试验找出了答案。

I had to add this piece of code: 我必须添加以下代码:

            GoogleClientSecrets cs = GoogleClientSecrets.Load(stream);

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

And then get a token. 然后获得令牌。 To get the token I followed the instructions here, under OAuth Playground: https://developers.google.com/adwords/api/docs/guides/authentication 要获取令牌,我按照此处的说明在OAuth游乐场下进行: https : //developers.google.com/adwords/api/docs/guides/authentication

And then used the information there to make this: 然后使用那里的信息进行此操作:

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

(I don't think the tokentype or expires in are important, but I included them anyway) (我不认为令牌类型或过期时间很重要,但无论如何我都将它们包括在内)

And finally: 最后:

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

Then this credential was used in the same way as before. 然后以与以前相同的方式使用此凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM