【发布时间】:2016-05-27 14:10:38
【问题描述】:
我尝试在 Windows 10 通用应用中实现谷歌登录。
我还在 azure 门户中使用 .net 后端将 google 应用程序客户端 ID 和密码添加到我的 azure 移动应用程序服务,并启用了 google 登录。 我安装了 AzureMobileService SDK 在 LoginPage.xaml.cs 中添加了以下代码
// Define a member variable for storing the signed-in user.
private MobileServiceUser user;
// Define a method that performs the authentication process
// using a Google sign-in.
private async System.Threading.Tasks.Task<bool> AuthenticateAsync()
{
string message;
bool success = false;
// This sample uses the Google provider.
var provider = MobileServiceAuthenticationProvider.Google;
// Use the PasswordVault to securely store and access credentials.
PasswordVault vault = new PasswordVault();
PasswordCredential credential = null;
try
{
// Try to get an existing credential from the vault.
credential = vault.FindAllByResource(provider.ToString()).FirstOrDefault();
}
catch (Exception)
{
// When there is no matching resource an error occurs, which we ignore.
}
if (credential != null)
{
// Create a user from the stored credentials.
user = new MobileServiceUser(credential.UserName);
credential.RetrievePassword();
user.MobileServiceAuthenticationToken = credential.Password;
// Set the user from the stored credentials.
App.MobileService.CurrentUser = user;
// Consider adding a check to determine if the token is
// expired, as shown in this post: http://aka.ms/jww5vp.
success = true;
message = string.Format("Cached credentials for user - {0}", user.UserId);
}
else
{
try
{
// Login with the identity provider.
user = await App.MobileService
.LoginAsync(provider);
// Create and store the user credentials.
credential = new PasswordCredential(provider.ToString(),
user.UserId, user.MobileServiceAuthenticationToken);
vault.Add(credential);
success = true;
message = string.Format("You are now logged in - {0}", user.UserId);
}
catch (MobileServiceInvalidOperationException)
{
message = "You must log in. Login Required";
}
}
var dialog = new MessageDialog(message);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
return success;
}
private async void ButtonLogin_Click(object sender, RoutedEventArgs e)
{
// Login the user and then load data from the mobile app.
if (await AuthenticateAsync())
{
var dialog = new MessageDialog("Loged in");
await dialog.ShowAsync();
}
}
并在 App.xaml.cs 中添加如下代码
// Connect to azure
public static MobileServiceClient MobileService = new MobileServiceClient("https://xxxxxxxxxxxxxxx.azurewebsites.net");
点击登录按钮后,谷歌签名窗口弹出,输入用户名和密码后,我尝试登录,显示以下消息
错误消息显示“我们现在无法连接到您需要的服务。请检查您的网络连接,稍后再试”
在 console.developers.google.com 中,我启用了我的 google app api,并将授权重定向 URI 添加为:http://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback
我该如何解决这个错误
【问题讨论】:
标签: c# azure uwp azure-mobile-services google-signin