Fre*_*kyB 8 c# google-login google-plus
我用的是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
}
Run Code Online (Sandbox Code Playgroud)
当然,我将服务器的地址添加到原始uri,还添加到Google控制台上的授权重定向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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
public static async Task<UserCredential> AuthorizeAsync
Run Code Online (Sandbox Code Playgroud)
此方法已在 GoogleWebAuthorizationBroker 中声明,因此如果您打算使此函数的实现优先于基本实现,则需要使用 new 关键字。
public new static async Task<UserCredential> AuthorizeAsync
Run Code Online (Sandbox Code Playgroud)
这就是为什么我假设您在记录之前停止了线路
UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync
Run Code Online (Sandbox Code Playgroud)
此时,它正在调用基础实现。
除此之外,我通常倾向于使用DotNetOpenAuth与 Google 交互,并且有很多简单的示例可供遵循,例如此处和此处.. 但如果您真的想仅使用 Google Apis 来推出自己的示例,那么这是最好的起点