适用于UWP的Google API在自定义重定向方案中效果不佳

时间:2018-07-22 05:51:41

标签: uwp google-oauth

我正在编写一个可以与Google Drive API一起使用的UWP应用程序。我的问题似乎是由于Google提供的关于UWP应用程序的不良文档。在文档中指出:

  

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

...在此之下,它指出:

  

对于UWP应用,该方案不能超过39个字符。

这里的问题是客户端ID已经超过39个字符。没有解决此问题的方法,也没有办法在Google Cloud Platform控制台内专门为UWP应用创建证书。我不拥有域,因此我无法对自定义重定向uri使用其他选项。我在文档中缺少什么吗?

以下是根据GitHub存储库上的示例在GitHub UWP Sample上重现在Visual Studio中设置自定义重定向uri的步骤:

  1. 打开package.appxmanifest文件。
  2. 转到“声明”标签。
  3. 添加协议声明。
  4. 当您尝试在“名称”字段中添加客户端ID时,会出现此错误:
  

验证错误。错误C00CE169:应用清单验证错误:应用清单必须按照架构有效:第32行,第25列,原因:“ com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”违反了“ 39”的maxLength约束。值“ com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”的属性“名称”无法解析。

因此,我很茫然。我无法将其他重定向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);
            }
        }
    }
}
}

任何对此的帮助将不胜感激。谢谢。

redirect_uri_mismatch

0 个答案:

没有答案