URI mismatch error 400 .NET, my code always points to a different URL

93 views Asked by At

At this point every information is welcomed. I'm trying to connect via OAuth2 to a Google Drive API, but when I run my credentials:

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore("TokenStore"),
                        new LocalServerCodeReceiver(RedirectUri)).Result;
}

I always get this error:

redirect_uri

The problem I have is that the url changes every time I call that part of code, so it is impossible to enter in the GoogleCloud Console a redirect url that matches. I have tried everything, setting the ports statically from VS, registering different authentication urls, I have checked my client id and the client secret and matches perfectly, at this point I don't really know where to go, since I haven't been able to set that url statically in any way.

Anny suggestions?

This is the full code:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DriveQuickstart
{
    public class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/drive-dotnet-quickstart.json
        //follow this link
        //https://developers.google.com/drive/v3/web/quickstart/dotnet

        static string[] Scopes = { DriveService.Scope.DriveReadonly };
        static string ApplicationName = "Drive API .NET Quickstart";

        public static void Drive()
        {
            try
            {
                UserCredential credential;

                const string RedirectUri = "http://localhost:61930/authorize/";

                using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore("TokenStore"),
                        new LocalServerCodeReceiver(RedirectUri)).Result;
                }

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

                // Define parameters of request.
                FilesResource.ListRequest listRequest = service.Files.List();
                listRequest.PageSize = 10;
                listRequest.Fields = "nextPageToken, files(id, name)";

                // List files.
                IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                     .Files;
                Console.WriteLine("Files:");

                if (files != null && files.Count > 0)
                {
                    foreach (var file in files)
                    {
                        Console.WriteLine("{0} ({1})", file.Name, file.Id);
                    }
                }
                else
                {
                    Console.WriteLine("No files found.");
                }

                Console.Read();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

I have tried everything, clearly the problem is that a url is generated dynamically and I cannot make it static, I don't know if it is my part of the code or it could be something in the project at a global level, but I have set the ports in a way static from the appsettings.json or web.config etc and nothing, at the point of validating credentials it redirects to another port, thanks everyone!

1

There are 1 answers

2
Linda Lawton - DaImTo On

The issue is mainly that you have code for an installed / desktop application yet you are using client_secret.json for a web application which is wrong. Go create the correct credential type.

If you really want to use web creds for some reason which I dont understand you can fix the redirect uri error by going to google developer console and add the redirect uri exactly as it is written here

enter image description here

How to fix redirect URI missmatch