问题描述
我使用以下代码使用Google .Net客户端库向Google进行身份验证。
I am using the following code to authenticate to Google using the Google .Net client library.
public static void auth()
{
string clientId = "xxxxxx.apps.googleusercontent.com";
string clientSecret = "xxxxx";
string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" }; // view your basic profile info.
try
{
// Use the current Google .net client library to get the Oauth2 stuff.
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, "test"
, CancellationToken.None
, new FileDataStore("test")).Result;
// Translate the Oauth permissions to something the old client libray can read
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.AccessToken = credential.Token.AccessToken;
parameters.RefreshToken = credential.Token.RefreshToken;
RunContactsSample(parameters);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
我使用自己的客户端ID和客户端密钥。
当我从visual studio运行时,这段代码完全正常工作,但是在IIS中托管之后无效。
I am using my own client id and client secret key.This code is perfectly working when I am running from visual studio, but is not working from after hosted in IIS.
我在google api中提到了重定向的URI控制台是
And I mentioned redirected URI in google api console ishttp://localhost/authorize/
我的IIS主机网址为
My IIS host Url ishttp://localhost/googleintegration.aspx
我在过去的一个月内遇到了这个问题,任何人都可以为此提供解决方案..
I am facing this issue by last one month, can anyone please give a solution for this..
推荐答案
(重新发布自)
GoogleWebAuthorizationBroker
用于在最终用户计算机上本地运行的应用程序。它会打开一个Web浏览器本地,允许用户进行身份验证。
听起来你试图在IIS中从服务器机器上使用 GoogleWebAuthorizationBroker
;它将无法工作,因为它将尝试在该服务器机器上打开一个Web浏览器 - 这将失败,并且无论如何最终用户都看不到它,因为它将在服务器机器,而不是他们的机器。
GoogleWebAuthorizationBroker
is for use in an application that is running locally on an end-user machine. It opens a web browser locally allowing the user to authenticate.It sounds like you are trying to use GoogleWebAuthorizationBroker
on a server machine, from within IIS; where it won't work, as it will try to open a web browser on that server machine - this will fail, and wouldn't be visible to the end-user anyway as it'll be on the server machine, not their machine.
我自己没有这样做,但它看起来像(对于ASP.NET MVC Web应用程序)可能会有帮助。
I've not done this myself, but it looks like the instructions here (for an ASP.NET MVC web app) might help.
这篇关于GoogleWebAuthorizationBroker无法从IIS主机运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!