For reference, I followed this site https://qawithexperts.com/article/asp-net/upload-file-to-google-drive-using-google-drive-api-in-aspnet/236 on how to upload a file to Google Drive in a Web Application.
This is my code to get the DriveService
public static string[] Scopes = { Google.Apis.Drive.v3.DriveService.Scope.Drive };
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
DriveService service = null;
//Root Folder of project
var CSPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
try
{
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))
{
String FolderPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
//create Drive API service.
service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveMVCUpload",
});
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.InnerException);
}
return service;
}
The problem occurs at getting the credential from GoogleWebAuthorizationBroker.AuthorizeAsync. When that line runs, I would be redirected to this link (The port of the redirect URI http://127.0.0.1:52829/authorize/ changes everytime I run this code).
I have looked into Google's Error docs and other stack overflow explaining that I must add the redirect URI on the console in which I did shown here but it still has the redirect_uri_mismatch error. When I do just open the redirect URI (example : http://127.0.0.1:52829/authorize/) given in the error, I get the following
. But then an exception would be thrown
Exception thrown: 'System.AggregateException' in mscorlib.dll
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at .........
One or more errors occurred.
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:""
at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.<AuthorizeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext()
**service** was null.
and I couldn't find much about this specific exception error :
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:"" but I believe this is because of the redirect mismatch URI problem.
For more information: The current link of the web application is http://localhost:61506 and the link when I run the code above is at http://localhost:61506/AutoFileTransfer/Index
So I don't really know why I get this redirect mismatch URI error nor do I know how to solve the
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:""
problem either.