我使用VS2015,C#。
我在使用Google登录时遇到问题。从我的调试配置(localhost)一切正常。发布到服务器后,谷歌登录窗口根本无法打开。并且没有例外。 这是我的代码:
[AllowAnonymous]
public async Task LoginWithGoogle()
{
HttpRequest request = System.Web.HttpContext.Current.Request;
string redirectUri = ConfigurationReaderHelper.GetGoogleRedirectUri();
try
{
ClientSecrets secrets = new ClientSecrets
{
ClientId = "***",
ClientSecret = "***"
};
IEnumerable<string> scopes = new[] { PlusService.Scope.UserinfoEmail, PlusService.Scope.UserinfoProfile };
GoogleStorageCredentials storage = new GoogleStorageCredentials();
dsAuthorizationBroker.RedirectUri = redirectUri;
UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync(secrets,
scopes, "", CancellationToken.None, storage);
}
catch(Exception ex)
{
throw ex;
}
}
//just getting value from applicationSettings - web.config
public static string GetGoogleRedirectUri()
{
#if DEBUG
return GetValueFromApplicationSettings("RedirectUriDEBUG");
#elif PRODUKCIJA
return GetValueFromApplicationSettings("RedirectUriSERVER");
#endif
}
当然,我将服务器的地址添加到原始uri以及谷歌控制台上的授权重定向uri以供开发人员使用。 (就像我为localhost所做的那样)。我只是不明白它出了什么问题,为什么登录窗口没有打开?
修改
添加类dsAuthorizationBroker(我的第一篇文章中遗漏了 - 对不起该帖子):
namespace Notes
{
public class dsAuthorizationBroker : GoogleWebAuthorizationBroker
{
public static string RedirectUri;
public static async Task<UserCredential> AuthorizeAsync(
ClientSecrets clientSecrets,
IEnumerable<string> scopes,
string user,
CancellationToken taskCancellationToken,
IDataStore dataStore = null)
{
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = clientSecrets,
};
return await AuthorizeAsyncCore(initializer, scopes, user,
taskCancellationToken, dataStore).ConfigureAwait(false);
}
private static async Task<UserCredential> AuthorizeAsyncCore(
GoogleAuthorizationCodeFlow.Initializer initializer,
IEnumerable<string> scopes,
string user,
CancellationToken taskCancellationToken,
IDataStore dataStore)
{
initializer.Scopes = scopes;
initializer.DataStore = dataStore ?? new FileDataStore(Folder);
var flow = new dsAuthorizationCodeFlow(initializer);
return await new AuthorizationCodeInstalledApp(flow,
new LocalServerCodeReceiver())
.AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
}
}
public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
public dsAuthorizationCodeFlow(Initializer initializer)
: base(initializer) { }
public override AuthorizationCodeRequestUrl
CreateAuthorizationCodeRequest(string redirectUri)
{
return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);
}
}
}
答案 0 :(得分:3)
public static async Task<UserCredential> AuthorizeAsync
此方法已在GoogleWebAuthorizationBroker中声明,因此如果您打算将此函数的实现优先于基本实现,则需要使用new关键字。
public new static async Task<UserCredential> AuthorizeAsync
这就是为什么我假设您在
之前停止记录行的原因UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync
此时,它正在调用基础实现。
除此之外,我通常倾向于使用DotNetOpenAuth与Google进行互动,并且有很多简单的示例可供使用,例如here和here ..但如果你真的想要要使用Google Apis滚动自己,这是最好的place to start