Access to the path Google.Apis.Auth is denied - c#

I was trying to implement google calender event sync functionality to schedule event from my local application to google calendar. its working fine in local system but whenever I deployed it on server, it throws the following error.
System.UnauthorizedAccessException: Access to the path 'Google.Apis.Auth' is denied
Below is the code which I am using for same.
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "xxx-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None).Result;
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = Summary,
});

By default the Google .Net client library uses FileDatastore() file datastore by default stores the credentials in %appData%. When you run your application under what ever user it is you are running you don't apperntly have access to %appData%
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "xxx4671204-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Make sure that credPath is some place that you have access to write to. I have a sample on GitHub for Google calendar Oauth2 and I have a tutorial on how filedatastore works FileDatastore demystified

Related

When run project show error : Access Block Error : Access blocked: "WebName" request is invalid

When i run my local MVC project show me error like this
Access blocked: GoogleDriveRestAPI_v3’s request is invalid
my code MVC code is here use in Models folder
`
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
using (var stream = new FileStream(#"D:\client_secret.json", FileMode.Open, FileAccess.Read))
{
String FolderPath = #"D:\";
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.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
`
when run credential its show error like mention in image
i use google cloud side generated secret client file like this
show allow access google drive to uploading file.

Google Drive Auth2: How to fix Error 400: redirect_uri_mismatch

I am trying to perform and auth2 request for google drive and am getting a redirecturi mismatch error.
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecret etc..
how do we set the redirecturi property to have the same redirect uris has defined when setting up the app?
It was mentioned the fix for Google Drive v2 but I am using v3 and these are not working.
I do not see a way, in the code, to set the google drive auth redirect uri.
There must be away.
Code is the following:
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile,};
var clientId = "txdjxtd.apps.googleusercontent.com";
var clientSecret = "didididijdiikj";
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
}, scopes,
Environment.UserName, CancellationToken.None, new FileDataStore("MyAppsToken")).Result;
Please let me know.
Thanks

Getting access token and refresh token without asking for user permission

I want to access Google Drive to upload files using Google.Apis.Drive.v3.
Here is my code:
protected async void btnConnectGoogleDrive_Click(object sender, EventArgs e)
{
string[] Scopes = { DriveService.Scope.Drive };
UserCredential credential;
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "",
ClientSecret = ""
},
Scopes,
"user",
CancellationToken.None);
InsertGoogleDriveToken(credential.Token.AccessToken, areaID, "accesstoken");
InsertGoogleDriveToken(credential.Token.RefreshToken, areaID, "refreshtoken");
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "SocialLadder",
});
GoogleDriveUpload(service, areaID);
}
Here I am able to get the AccessToken and RefreshToken but user is not redirected to permission page, so when I try to upload images to drive it gives me error that "permission_not_granted". Used same thing in MVC and that works great.
Please help me with this issue.
I suspect that you have changed your scopes. You need to authenticate the user. Do one of the following
Go to %appdata% and delete the creditlas file for this user.
change "user" to something else say "user1"
Your application should then require the user to authenticate again.

Google Spreadsheets Api Key example

I'm developing an application wich needs to access a "simple database" (google spreadsheet) with users IDs.
I try with Oauth2.0 But that's not what I need.
I'm using this code to get access to the sheet:
private static SheetsService AuthorizeGoogleApp()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
But with this code the C# application open a browser to ask the user to login in his google account.
I need to have access via API, so it will be transparent to the user.
I already have the api key, but I don't know how to use and I don't find any documentation on Google sites.
Can you help me with some kind of example to read a simple column?
Thanks in advance!
Here is the documentation for the API.
It also covers authorization.

List of Google Plus People contacts returns 0

I want to fetch the list of people in all the circles of a user's Google plus account.
I am using the Google People API but the list always returns 0.
The code is as below:
string[] scopes = new string[] {PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile};
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
UserCredential credential =
GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = "someid",
ClientSecret = "somekey"
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore(credPath,true)
).Result;
PlusService service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Plus Sample",
});
PeopleResource.ListRequest listPeople = service.People.List("me", PeopleResource.ListRequest.CollectionEnum.Visible);
listPeople.MaxResults = 10;
PeopleFeed peopleFeed = listPeople.Execute();
If you check the documentation for people.list you can see that this method doesnt work anymore.
The Google+ People API list endpoint is deprecated. Consider using the Google People API instead.
You can try and switch to the google people api but this is not a list of the users on Google+ IIR its a list of peolpe the user has setup in Google contacts.

Categories