Google APIs for UWP not playing nice with custom redirect scheme

牧云@^-^@ 提交于 2019-12-07 19:02:49

问题


I'm writing a UWP application that will work with the Google Drive APIs. My issue seems to be out of poor documentation from Google for UWP applications. In the documentation it states:

Additionally, you can use the reverse DNS notion of the client ID as the custom URI scheme (e.g. com.googleusercontent.apps.123).

...And right below that, it states:

For UWP apps, the scheme cannot be longer than 39 characters.

The problem here is that the client IDs are already above 39 characters. There is no remedy for this issue, and there is no way to create a credential specifically for UWP apps within the Google Cloud Platform console. I don't own a domain, so there would be no way for me to use the other option for a custom redirect uri. Am I missing something in the documentation?

Here are the steps to reproduce setting up the custom redirect uri in visual studio, per the samples on the GitHub repo, here: GitHub UWP Sample:

  1. Open the package.appxmanifest file.
  2. Go to the Declarations tab.
  3. Add a protocol declaration.
  4. When you attempt to add the client ID in the 'Name' field, it will give this error:

Validation error. error C00CE169: App manifest validation error: The app manifest must be valid as per schema: Line 32, Column 25, Reason: 'com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' violates maxLength constraint of '39'. The attribute 'Name' with value 'com.googleusercontent.apps.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' failed to parse.

I'm at a loss because of this. I am unable to add other redirect URIs to the API console, since it is of type "Other". I have tried to add a Web API credential and Google returns an error saying that custom redirect URIs are not allowed with that type.

Here is the code from the app itself:

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);
            }
        }
    }
}
}

Any help with this would be greatly appreciated. Thanks.

redirect_uri_mismatch

来源:https://stackoverflow.com/questions/51462403/google-apis-for-uwp-not-playing-nice-with-custom-redirect-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!