I am calling the following code on my back end when a user want to access their google drive through my service.
public static DriveService GetService(string app_userID)
{
//get Credentials from client_secret.json file
UserCredential credential;
var path = HttpContext.Current.Server.MapPath(@"~/client_secret.json");
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = HttpContext.Current.Server.MapPath(@"~/token.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
app_userID,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
//create Drive API service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
The problem though is that I think this code is developed to run on the front end app. I get the following error and I think the program tries to open up a web page for authentication but on the back end server.
{
"Message": "An error has occurred.",
"ExceptionMessage": "One or more errors occurred.",
"ExceptionType": "System.AggregateException",
"StackTrace": " at System.Threading.Tasks.Task`1.GetResultCore(Boolean .......GoogleDrive.GoogleDriveFilesRepository.GetService(String tt_userID) in ,,,
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Failed to launch browser with \"...(google url)..." for authorization. See inner exception for details.",
"ExceptionType": "System.NotSupportedException",
"StackTrace": " ...
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Access is denied",
"ExceptionType": "System.ComponentModel.Win32Exception",
"StackTrace": " at ...
How do I change so that the end user are authenticated on the client side instead? See my set up below.
Note that I still would like to be able to call the API from the back end if possible.