Google Site Verification API .NET redirect uri is not adopted

1k Views Asked by At

(http deleted because of reputation) I'm testing the Google Site Verification API with the "GoogleApisSamples" Projects from Google, but I have a problem concerning the redirect uri.. I get the client_secrets.json (with redirect uris set) from my GoogleDrive Application, but the redirect uri that this programm gets is something like "localhost:1168/authorize/" (it changes). I set the redirect uri to "www.google.com" and "www.google.com/".

namespace SiteVerification.VerifySite

{

internal class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        // Display the header and initialize the sample.
        Console.WriteLine("Site Verification sample");
        Console.WriteLine("========================");

        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { SiteVerificationService.Scope.Siteverification },
                "user", CancellationToken.None, new FileDataStore("SiteVerification.VerifySite")).Result;
        }

        // Create the service.
        var service = new SiteVerificationService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "SiteVerification API Sample",
            });
        RunVerification(service);

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }

    /// <summary>
    /// This method contains the actual sample code.
    /// </summary>
    private static void RunVerification(SiteVerificationService service)
    {
        // Request user input.
        Console.WriteLine("Please enter the URL of the site to verify:");
        var site = Console.ReadLine();
        Console.WriteLine();

        // Example of a GetToken call.
        Console.WriteLine("Retrieving a meta token ...");
        var request = service.WebResource.GetToken(new SiteVerificationWebResourceGettokenRequest()
        {
            VerificationMethod = "meta",
            Site = new SiteVerificationWebResourceGettokenRequest.SiteData()
            {
                Identifier = site,
                Type = "site"
            }
        });
        var response = request.Execute();
        Console.WriteLine("Token: " + response.Token);
        Console.WriteLine();

        Console.WriteLine("Please place this token on your webpage now.");
        Console.WriteLine("Press ENTER to continue");
        Console.ReadLine();
        Console.WriteLine();

        // Example of an Insert call.
        Console.WriteLine("Verifying...");
        var body = new SiteVerificationWebResourceResource();
        body.Site = new SiteVerificationWebResourceResource.SiteData();
        body.Site.Identifier = site;
        body.Site.Type = "site";
        var verificationResponse = service.WebResource.Insert(body, "meta").Execute();

        Console.WriteLine("Verification:" + verificationResponse.Id);
        Console.WriteLine("Verification successful!");
    }
}

}

And my "client_secrets.json" (I changed the Stuff in caps)

{
"web": {
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "client_secret": "CLIENT_SECRET",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "client_email": "STUFF",
    "redirect_uris": [
        "http://www.google.com/",
        "http://www.google.com"
    ],
    "client_x509_cert_url": "STUFF",
    "client_id": "CLIENT_ID",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "javascript_origins": [
        "https://www.google.com"
    ]
}

}

And the error I get is:

  1. That’s an error.

Error: redirect_uri_mismatch

Application: GoogleApisSamples

The redirect URI in the request: localhost:1168/authorize/ did not match a registered redirect URI.

2

There are 2 best solutions below

5
On BEST ANSWER

Redirect URI must match the location you want the Authentication to be returned to

For a Client ID for native application you could set it to the following:

 Redirect URIs     urn:ietf:wg:oauth:2.0:oob  
                   http://localhost

For Client ID for web application it would be something more like this

    Redirect URIs     
         http://localhost/google-api-php-client-samples/oauth2.php 

Web must patch to a actual file.

This example might be easer to use the loading the file into a stream.

string[] scopes = new string[] { SiteVerificationService.Scope.Siteverification };
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
                                           {
                                            ClientId = CLIENT_ID,
                                            ClientSecret = CLIENT_SECRET
                                             },
                   scopes,
                   Environment.UserName,
                   CancellationToken.None,
                   new FileDataStore"Daimto.SiteVerification.Auth.Store")).Result;

// Create the service.
var service = new SiteVerificationService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "SiteVerification API Sample",
            });
2
On

Thanks for this answer. This is the only place I think it isclearly mentioned that for a web application type, the redirect_uri has to map to the actual file. I use an ASP.Net MVC application and was giving the action as the redirect_uri (Wrong). When I changed it to the actual *.cshtml file, it all worked!!!