skip to Main Content

I am try to connect google drive api with my asp.net core 3.1 web server which run on http://127.0.0.1:4000 .But during authorizing I get different redirect_uri. And the credential.json is all valid.

The error I get:

enter image description here

My code:

public class GDriveService : BaseService
    {
        private string credentialFileName = ConfigurationManager.AppSettings["CredentialFile"].ToString();
        private string appName = ConfigurationManager.AppSettings["AppName"].ToString();
        private string[] scopes;
        private UserCredential credential;
        private DriveService service;
        public GDriveService(IWebHostEnvironment env):base(env)
        {
            scopes = new string[] { DriveService.Scope.Drive,
                               DriveService.Scope.DriveFile,};


            using (var stream = new FileStream(Path.Combine(env.WebRootPath, credentialFileName), FileMode.Open, FileAccess.Read))
            {
                String FilePath = Path.Combine(env.WebRootPath, "DriveServiceCredentials");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(FilePath, true)).Result;
            }
}

My credential configuration:

enter image description here

2

Answers


  1. The redirect uri you are sending from must exactly match the one that you have registered. In your case you are sending from. (Note this is controlled by the client library)

    enter image description here

    You have only registered the following in Google cloud console

    enter image description here

    The solution is to add the redirect uri that the error message is telling you exactly including the ending /.

    This video will show you exactly how to fix it. Google OAuth2: How the fix redirect_uri_mismatch error. Part 2 server sided web applications.

    Remember to ensure that Visual studio is using a static port. If your port keeps changing your redirect uri must change as well.

    Login or Signup to reply.
  2. Try to add http://127.0.0.1/ and http://127.0.0.1/authorize/ to the Authorized redirect URIs section.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search