Search code examples
asp.net-mvcoauth-2.0google-apigoogle-oauthgoogle-sheets-api

Google Oauth 2.0 Web API redirect_uri_mismatch - Google redirecting the wrong URL


I am trying to use Google API login for Google sheets in asp.net c# project but getting redirect_uri_mismatch error 400. I didn't even set this redirect URL which Google is redirecting to. I have set redirect url http://127.0.0.1:63092/login.aspx but Google is redirecting to http://127.0.0.1:13557/authorize/ wrong port and wrong web page. My Credential.json also not having anything like /authorize/ as you can see below.

{
  "web": {
    "client_id": "client_idxxxxxxx.apps.googleusercontent.com",
    "project_id": "app-xxxxx",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "secret_xxxxxxxx",
    "redirect_uris": [ "http://127.0.0.1:63092/login.aspx" ],
    "javascript_origins": [ "http://127.0.0.1:63092" ]
  }
}

On every try, it changes the port on 127.0.0.1 and redirecting to /authorize/ what could be the issue here? What am I doing wrong?

enter image description here

My Code

static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "My App";
static string thisDir = System.Web.Hosting.HostingEnvironment.MapPath("~/");
protected void Button1_Click(object sender, EventArgs e)
{
    UserCredential credential;
    using (var stream =
    new FileStream(thisDir + "credential.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = thisDir + "token.json";
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
    }

    var service = new SheetsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    String spreadsheetId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    String range = "Sheet1";
    SpreadsheetsResource.ValuesResource.GetRequest request =
            service.Spreadsheets.Values.Get(spreadsheetId, range);
    ValueRange response = request.Execute();
    IList<IList<Object>> values = response.Values;
    if (values != null && values.Count > 0)
    {
        Console.WriteLine("Name, Major");
        foreach (var row in values)
        {
            Console.WriteLine("{0}, {1}", row[0], row[4]);
        }
    }
    else
    {
        Console.WriteLine("No data found.");
    }
}

Solution

  • Google offers serveral credential types for different application usage. In your case, you are using credentials designed for web servers instead of credentials designed for installed applications (running locally at someone's desktop).

    In the Google Credentials console, select "Other" for the Application Type. Download the new credentials and try again.