【发布时间】:2020-01-15 15:09:21
【问题描述】:
开始 - 我已在此处 (https://github.com/googlesamples/oauth-apps-for-windows/tree/master/OAuthUniversalApp) 获取 Google 提供的示例 UWP 应用。
我已经成功地使用了我自己的 clientID(设置记录在此处的答案中:How to create a custom OAuth 2.0 redirect_uri for Google provider, for UWP app?)。重定向回应用程序的工作正常 - 并从示例中的 OnNavigatedTo() 方法触发。
然后我有一个 UWP,它记录传感器的温度(最终将在 Raspberry Pi 上运行) - 但现在我在普通 Windows 10 上运行它。我已将示例中的 Oauth 位复制到我的项目中- 并且还在 Package.appxmanifest 中使用名称“pw.oauth2”声明了协议(与示例相同,也与我在 Google 开发者控制台中的内容相匹配)。
当我启动应用并点击“使用 Google 登录”时,它会成功启动浏览器,请求授权/登录,然后重定向回应用。我知道这部分正在工作,因为它将应用程序带回前台。但是,返回我的应用程序时,OnNavigatedTo() 方法没有触发。
我已经搜索并阅读了相关文档 - 但无法在我的应用程序中触发该方法。我觉得我错过了在我的应用程序中未配置的示例应用程序中设置的其他内容。任何帮助或建议表示赞赏!
这是我的 XAML:
<Page
x:Class="CamIOT.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CamIOT"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<RelativePanel>
<Button x:Name="button" HorizontalAlignment="Left" Margin="18,22,0,0" VerticalAlignment="Top" Width="206" Height="46" Click="oauth_Click"
BorderThickness="0" Padding="0" Background="Transparent">
<Image Source="Assets/btn_google_sign-in.png" Stretch="UniformToFill" HorizontalAlignment="Left" Width="206"/>
</Button>
<TextBox x:Name="textBoxOutput" TextWrapping="Wrap" Text=""
IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="300,40,0,0" Height="731" Width="1000"/>
<ScrollViewer Margin="12,100,12,12">
<StackPanel>
<TextBlock TextWrapping="Wrap">
I2C Device Data
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,10,0,0">
Temperature and Humidity Data
</TextBlock>
<ScrollViewer HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto">
<Image Source="Assets/i2c_sample_bb.jpg" Stretch="None" Margin="2,10,2,0" />
</ScrollViewer>
<Button x:Name="StartStopButton" Content="Start" Margin="0,10,0,0" Click="{x:Bind StartStopScenario}"/>
<TextBlock x:Name="ScenarioControls" Visibility="Collapsed">
Current Temperature: <Run x:Name="CurrentTemp"/>
<LineBreak/>
Current Humidity: <Run x:Name="CurrentHumidity"/>
</TextBlock>
</StackPanel>
</ScrollViewer>
</RelativePanel>
</Grid>
</Page>
这是我的 OnNavigatedTo() 方法:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedTo Fired!");
if (e.Parameter is Uri)
{
// Gets URI from navigation parameters.
Uri authorizationResponse = (Uri)e.Parameter;
string queryString = authorizationResponse.Query;
output("MainPage received authorizationResponse: " + authorizationResponse);
// Parses URI params into a dictionary
// ref: http://stackoverflow.com/a/11957114/72176
Dictionary<string, string> queryStringParams =
queryString.Substring(1).Split('&')
.ToDictionary(c => c.Split('=')[0],
c => Uri.UnescapeDataString(c.Split('=')[1]));
if (queryStringParams.ContainsKey("error"))
{
output(String.Format("OAuth authorization error: {0}.", queryStringParams["error"]));
return;
}
if (!queryStringParams.ContainsKey("code")
|| !queryStringParams.ContainsKey("state"))
{
output("Malformed authorization response. " + queryString);
return;
}
// Gets the Authorization code & state
string code = queryStringParams["code"];
string incoming_state = queryStringParams["state"];
// Retrieves the expected 'state' value from local settings (saved when the request was made).
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string expected_state = (String)localSettings.Values["state"];
// Compares the receieved state to the expected value, to ensure that
// this app made the request which resulted in authorization
if (incoming_state != expected_state)
{
output(String.Format("Received request with invalid state ({0})", incoming_state));
return;
}
// Resets expected state value to avoid a replay attack.
localSettings.Values["state"] = null;
// Authorization Code is now ready to use!
output(Environment.NewLine + "Authorization code: " + code);
string code_verifier = (String)localSettings.Values["code_verifier"];
performCodeExchangeAsync(code, code_verifier);
}
else
{
Debug.WriteLine(e.Parameter);
}
}
关于我缺少什么以及为什么 OnNavigatedTo() 在示例应用程序中可以正常触发,但在我的应用程序中不能正常触发的任何想法?
提前感谢您的帮助!
【问题讨论】:
标签: c# uwp oauth google-authentication