我正在编写一个可与Google Drive API配合使用的UWP应用程序 . 我的问题似乎来自谷歌针对UWP应用程序的糟糕文档 . 在文档中它说明:
此外,您可以使用客户端ID的反向DNS概念作为自定义URI方案(例如com.googleusercontent.apps.123) .
......就在下面,它说:
对于UWP应用程序,该方案不能超过39个字符 .
这里的问题是客户端ID已超过39个字符 . 此问题没有任何补救措施,并且无法在Google Cloud Platform控制台中专门为UWP应用创建凭据 . 我没有域名,因此我无法使用其他选项进行自定义重定向uri . 我在文档中遗漏了什么吗?
以下是根据GitHub存储库中的示例重现在visual studio中设置自定义重定向uri的步骤,此处:GitHub UWP Sample:
-
打开package.appxmanifest文件 .
-
转到声明选项卡 .
-
添加协议声明 .
-
当您尝试在'Name'字段中添加客户端ID时,它将显示以下错误:
验证错误 . 错误C00CE169:应用清单验证错误:应用清单必须按架构有效:第32行,第25列,原因:'com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'违反了'39'的maxLength约束 . 值为“com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”的属性“名称”无法解析 .
我因此而感到茫然 . 我无法将其他重定向URI添加到API控制台,因为它的类型为“其他” . 我尝试添加Web API凭据,Google会返回错误消息,指出该类型不允许使用自定义重定向URI .
以下是应用程序本身的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Net.Http;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using Windows.UI.Core;
using Windows.Data.Json;
// The Blank Page item template is documented at
https://go.microsoft.com/fwlink/?LinkId=234238
namespace AllDrive
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ConnectionManager : Page
{
public static string httpResponseWebCode = "";
public ConnectionManager()
{
this.InitializeComponent();
SelectService.Items.Add("Google");
SelectService.Items.Add("Microsoft");
SelectService.Items.Add("DropBox");
ConnectionList.Items.Add("randomemail@gmail.com");
ConnectionList.Items.Add("randomemail@outlook.com");
ConnectionList.Items.Add("DropBox:randomusername");
AuthenticateWebView.Visibility = Visibility.Collapsed;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(MainPage));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedTo Fired!");
try
{
base.OnNavigatedTo(e);
this.Frame.BackStack.RemoveAt(this.Frame.BackStack.Count - 1);
} catch (System.ArgumentOutOfRangeException ex)
{
Debug.WriteLine("No other frames to close");
Debug.WriteLine(ex.StackTrace);
}
}
private void ServiceSelected(object sender, SelectionChangedEventArgs e)
{
HttpClientRequestAsync(0);
}
private void HttpClientRequestAsync(int mode)
{
if (mode == 0)
{
// Always catch network exceptions for async methods
HttpClient client = new HttpClient();
System.Net.Http.HttpResponseMessage httpResponse = new System.Net.Http.HttpResponseMessage();
try
{
int index = SelectService.SelectedIndex;
string codeVerifier = "top secret";
string codeChallenge = codeVerifier;
string redirectURI = "com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:/oauth2redirect";
Uri requestURI = new Uri("https://accounts.google.com/o/oauth2/v2/auth?" +
"scope=https://www.googleapis.com/auth/drive%20https://www.googleapis.com/auth/drive.appdata%20https://www.googleapis.com/auth/drive.file%20https://www.googleapis.com/auth/drive.metadata%20https://www.googleapis.com/auth/drive.scripts%20profile%20email&" +
"response_type=code&" +
"state=secret&" +
"redirect_uri=" + System.Uri.EscapeDataString(redirectURI) + "&" +
"client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com");
//Send the GET request
var success = Windows.System.Launcher.LaunchUriAsync(requestURI);
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
}
}
}
}
}
任何有关这方面的帮助将不胜感激 . 谢谢 .