7

I have implemented google drive functionality for file management its working fine in local system but whenever i hosted it on Godaddy server it throw following error

System.UnauthorizedAccessException Access to the path 'Google.Apis.Auth' is denied.

Following code i am using :

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
   new ClientSecrets
   {
       ClientId = System.Configuration.ConfigurationManager.AppSettings["GDriveClientId"],//Get ClientID from web.config.
       ClientSecret = System.Configuration.ConfigurationManager.AppSettings["GDriveClientSecret"]//Get ClientSecret from web.config.
   },
   new[] { DriveService.Scope.Drive },
   System.Configuration.ConfigurationManager.AppSettings["GDriveCreatedByUser"],//Get UserName from web.config.
   CancellationToken.None).Result;

return credential;

I am using VS2010,IIS 7 for above functionality

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Chandrika Prajapati
  • 867
  • 1
  • 6
  • 11

3 Answers3

8

Expanding what Chandrika has already said, the ASP.NET user needs read and write permissions to the Google API Client OAuth2 Library's permanent storage folder .

Its default value is a folder named "Google.Apis.Auth" in Environment.SpecialFolder.ApplicationData (which usually corresponds to C:\Users\your-user-name\AppData\Roaming).

Alternatively, another folder can be provided as the last parameter of the GoogleWebAuthorizationBroker.AuthorizeAsync() method:

var folder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
   new ClientSecrets
   {
       ClientId = "PutYourClientIdHere",
       ClientSecret = "PutYourClientSecretHere"
   },
   new[] { DriveService.Scope.Drive },
   "user",
   CancellationToken.None,
   new FileDataStore(folder)).Result;

return credential;

See: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#credentials and https://developers.google.com/accounts/docs/OAuth2

f.cipriani
  • 3,357
  • 2
  • 26
  • 22
  • Adding to the Google credentials call the 5th parameter 'new FileDataStore(folder)' solved the problem. Thanks! – Ricardo stands with Ukraine Mar 11 '15 at 16:11
  • I added that folder as a relative path, and code hangs at `AuthorizeAsync` method. Any idea? – Saeed Neamati Jul 07 '15 at 11:04
  • What is that `GDriveCreatedByUser`? – Saeed Neamati Jul 07 '15 at 11:12
  • @SaeedNeamati the third parameter is the key that the OpenAuth API uses to see if there's already a TokenResponse available in the store (mostly like a cache key). Actually the name I have used might be confusing, I have updated the example, now it should be more clear. – f.cipriani Jul 07 '15 at 13:13
  • Even doing this throws the exception, can there be any more reasons? I have also tried an in-memory data store that doesn't even interact with any file but still throws the same Access Denied issue. – Ali Baig Sep 26 '16 at 21:52
  • i have also tried above sample but getting "Access Denied" error any suggestions please? – SHEKHAR SHETE Jun 21 '17 at 09:10
3

Root cause of the problem : This issue is generated because after validating authentication request its create directory and token file under in window's user's folder and we have not right's for that folder of Godadday server so it was not working

Solution : Modified the source code of google apis[filedatasource.cs] for creating that file inside our directory and add reference of it and it will work

Chandrika Prajapati
  • 867
  • 1
  • 6
  • 11
  • sample code please? i have tried with var googleaccesstoken = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage"); but no luck – SHEKHAR SHETE Jun 21 '17 at 09:10
0

I think you will find a solution here: Deploying ASP.NET to Windows Azure cloud, application gives error when running on cloud.

You just need to configure your IIS to work with the FileDataStore.

The following is copied from the answer there:

A.If you have RDP Acces to the Azure cloud then change the IIS settings

1.Go to the IIS
2.Under sites select the Default site
3.Add Permission
4.choose I_User object and give read/write access.
5.later you can automate this setting using a batch file and startup task.

B.I think you are using any local path. You should change this to local storage for temporary requirement and blob storage for long requirement.

Community
  • 1
  • 1
peleyal
  • 3,472
  • 1
  • 14
  • 25
  • 1
    Thanks to all of you for your valuable response. my issue is solved and now its working fine in also GoDadday server after modified source code of api of google drive – Chandrika Prajapati May 06 '14 at 07:28
  • What did you have to change? – peleyal May 06 '14 at 12:48
  • Change "Environment.SpecialFolder.ApplicationData" with "System.Web.HttpContext.Current.Server.MapPath("YourFolderName")" in this line folderPath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folder); – Chandrika Prajapati May 08 '14 at 09:04
  • @ChandrikaPrajapati I tried to do the integration as you said. But I get an Access Denied error in this. Do you have any idea why it is coming like this. – Mukesh Feb 28 '17 at 08:50