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:
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:
2
Answers
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)
You have only registered the following in Google cloud console
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.
Try to add
http://127.0.0.1/
andhttp://127.0.0.1/authorize/
to the Authorized redirect URIs section.