Error 400: redirect_uri_mismatch - Google cloud api

540 views Asked by At

I have just setup a new project and added the following scopes for "Web application". email, and send mail

enter image description here

I have also enabled GMailAPI from library

After this I have created credentials. Then edit > redirect_uri

I am not sure what this uri should be but I have tried almost everything here

  1. Gmail error message state "If you are a developer of this app see error details"

    the url mentioned here : http:\x.x.x.x:1234\authorize\

    with and without ending slash

P.S: when I type above uri in my browser, I get to a break point in my application

  1. my home page url

    http:\localhost\default.aspx

  2. my calling page uri

    http:\localhost\member\create.aspx

None of these work and I still get redirect_uri_mismatch Access Blocked error

My code is still running on my local machine and not available in google cloud.

vb.net code

Dim credential As UserCredential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets With {
    .ClientId = "xxx",
    .ClientSecret = "xxx"
    },
    {"https://www.googleapis.com/auth/gmail.send"},
    "user",
    CancellationToken.None)

enter image description here

Update

I got to know that AuthorizeAsync is for installed applications and not for web apps, here is my updated code...which is not sending me back a token.

Public Function DoOauthAndSendEmail(subject As String, body As String, recipients As String) As Task

    Dim fromEmail As String = ConfigurationSettings.AppSettings("ContactEmail")

    Dim MailMessage As MailMessage = New MailMessage(fromEmail, recipients, subject, body)
    'Specify whether the body Is HTML
    MailMessage.IsBodyHtml = True

    'Convert to MimeMessage
    Dim Message As MimeMessage = MimeMessage.CreateFromMailMessage(MailMessage)
    
    
    Dim rawMessage As String = Message.ToString()

    Dim flow As IAuthorizationCodeFlow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer With {
.ClientSecrets = New ClientSecrets With {
    .ClientId = "CLIENT_ID",
    .ClientSecret = "CLIENT_SECRET"
},
    .Scopes = {GmailService.Scope.GmailSend}
    })

    

    Dim token As Responses.TokenResponse = New Responses.TokenResponse() 

    If flow IsNot Nothing And token IsNot Nothing Then
        Dim credential As UserCredential = New UserCredential(flow, "user", token)
        Dim success As Boolean = credential.RefreshTokenAsync(CancellationToken.None).Result

        Dim gmail As GmailService = New GmailService(New Google.Apis.Services.BaseClientService.Initializer() With {
          .ApplicationName = "APP_NAME",
          .HttpClientInitializer = credential
      })

        gmail.Users.Messages.Send(New Message With {
        .Raw = Base64UrlEncode(rawMessage)
    }, "me").Execute()

    End If

End Function
1

There are 1 answers

5
Linda Lawton - DaImTo On

You are looking in the wrong place for the redirect uri's it is found under credentials then edit your web app client

enter image description here

Google OAuth2: How the fix redirect_uri_mismatch error. Part 2 server sided web applications

update Installed app

The code you are using GoogleWebAuthorizationBroker.AuthorizeAsync is used for authorizing an installed application. In this instance you need to make sure that you have created an installed application credentials on google cloud console.

How to create installed application credetilas.

You should not be seeing a redirect uri error if you have created the correct credentials type for you to match the code you are using.