用于UWP的Google API不能很好地使用自定义重定向方案

用于UWP的Google API不能很好地使用自定义重定向方案,uwp,google-oauth,Uwp,Google Oauth,我正在编写一个UWP应用程序,它将与GoogleDriveAPI一起工作。我的问题似乎是由于谷歌关于UWP应用程序的糟糕文档。它在文件中指出: 此外,您可以使用客户端ID的反向DNS概念作为自定义URI方案(例如com.googleusercontent.apps.123) …就在下面,它说: 对于UWP应用程序,方案长度不能超过39个字符 这里的问题是客户端ID已经超过39个字符。这个问题没有解决办法,也没有办法在谷歌云平台控制台中专门为UWP应用程序创建凭据。我没有自己的域,所以我没有办法对

我正在编写一个UWP应用程序,它将与GoogleDriveAPI一起工作。我的问题似乎是由于谷歌关于UWP应用程序的糟糕文档。它在文件中指出:

此外,您可以使用客户端ID的反向DNS概念作为自定义URI方案(例如com.googleusercontent.apps.123)

…就在下面,它说:

对于UWP应用程序,方案长度不能超过39个字符

这里的问题是客户端ID已经超过39个字符。这个问题没有解决办法,也没有办法在谷歌云平台控制台中专门为UWP应用程序创建凭据。我没有自己的域,所以我没有办法对自定义重定向uri使用另一个选项。我在文档中遗漏了什么吗

下面是在visual studio中根据GitHub repo上的示例重新创建自定义重定向uri的步骤,如下所示:

  • 打开package.appxmanifest文件
  • 转到“声明”选项卡
  • 添加一个协议声明
  • 当您尝试在“名称”字段中添加客户端ID时,将出现以下错误:
  • 验证错误。错误C00CE169:应用程序清单验证错误:根据架构,应用程序清单必须有效:第32行第25列,原因:“com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”违反了“39”的maxLength约束。无法分析值为“com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”的属性“Name”

    我为此感到茫然。我无法将其他重定向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);
                }
            }
        }
    }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.IO;
    使用System.Linq;
    使用System.Runtime.InteropServices.WindowsRuntime;
    使用System.Net.Http;
    使用Windows基金会;
    使用Windows。
    使用Windows.UI.Xaml;
    使用Windows.UI.Xaml.Controls;
    使用Windows.UI.Xaml.Controls.Primitives;
    使用Windows.UI.Xaml.Data;
    使用Windows.UI.Xaml.Input;
    使用Windows.UI.Xaml.Media;
    使用Windows.UI.Xaml.Navigation;
    使用系统诊断;
    使用System.Threading.Tasks;
    使用系统线程;
    使用Windows.UI.Core;
    使用Windows.Data.Json;
    //空白页项模板被记录在
    https://go.microsoft.com/fwlink/?LinkId=234238
    名称空间AllDrive
    {
    /// 
    ///可以单独使用或在框架内导航到的空页。
    /// 
    公共密封部分类连接管理器:第页
    {
    公共静态字符串httpResponseWebCode=“”;
    公共连接管理器()
    {
    this.InitializeComponent();
    选择service.Items.Add(“谷歌”);
    选择service.Items.Add(“Microsoft”);
    选择service.Items.Add(“DropBox”);
    ConnectionList.Items.Add(“randomemail@gmail.com");
    ConnectionList.Items.Add(“randomemail@outlook.com");
    ConnectionList.Items.Add(“DropBox:randomusername”);
    AuthenticateWebView.Visibility=可见性。已折叠;
    }
    私有无效按钮\u单击(对象发送者,路由目标e)
    {
    框架导航(类型(主页));
    }
    受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
    {
    Debug.WriteLine(“onnavigatedtofired!”);
    尝试
    {
    基地。导航到(e);
    this.Frame.BackStack.RemoveAt(this.Frame.BackStack.Count-1);
    }捕获(System.ArgumentOutOfRangeException ex)
    {
    Debug.WriteLine(“没有其他要关闭的帧”);
    Debug.WriteLine(例如StackTrace);
    }
    }
    已选择专用void服务(对象发送方,SelectionChangedEventArgs e)
    {
    HttpClientRequestAsync(0);
    }
    私有无效HttpClientRequestAsync(整数模式)
    {
    如果(模式==0)
    {
    //始终捕获异步方法的网络异常
    HttpClient=新的HttpClient();
    System.Net.Http.HttpResponseMessage httpResponse=new System.Net.Http.HttpResponseMessage();
    尝试
    {
    int index=SelectService.SelectedIndex;
    string codeVerifier=“绝密”;
    字符串codeChallenge=代码验证程序;
    string redirectURI=“com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:/oauth2redirect”;
    Uri requestURI=新Uri(“https://accounts.google.com/o/oauth2/v2/auth?" +
    “范围=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&" +
    “响应类型=代码&”+
    “国家=秘密&”+
    “redirect_uri=“+System.uri.EscapeDataString(redirectURI)+”&”+
    “client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com”);
    //发送GET请求
    var success=Windows.System.Launcher.launchurisync(requestURI);
    }
    捕获(例外情况除外)
    {
    Debug.WriteLine(例如StackTrace);
    }
    }
    }
    }
    }
    
    在此方面的任何帮助都将不胜感激。谢谢


    从您的步骤中,我可以看出您为URI添加了协议。它与google api有关吗?问题是协议字段只允许39个字符,我必须将整个客户端id放入其中。我不确定URI方案中是否允许使用句号。尝试丢失“com.googleusercontent.apps”,只提供客户端的数字部分ID@pinoyyid它不能以数字开头。它的开头需要一个小写字母