0

我在生成令牌时遇到了一些与Google API有关的问题。该代码在本地机器上正常工作,但发布版本提供“访问被拒绝”错误。我知道这必须与文件夹的权限有关,但我不知道如何解决它。其实我们的授权功能是这样的:Google API授权访问在生成令牌时被拒绝

public static DriveService AuthenticateOauth(string clientId, string clientSecret, string userName) 
{ 
     String folder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage/"); 

     string[] scopes = new string[] { DriveService.Scope.Drive }; 

     // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% 
     UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } 
       , scopes 
       , userName 
       , CancellationToken.None 
       , new FileDataStore(folder)).Result; 

     DriveService service = new DriveService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = "Drive API", 
     }); 
     return service; 
} 

该网站是写在ASP NET MVC 5和Azure的网站托管。

谢谢你的帮助。

+0

您是否在[Google Developer Console](https://console.developers.google.com/apis/credentials)上为您的Production环境设置了正确的凭据? –

+0

是的,我有他们的代码,并在当地正常工作。我想在发布版本中必须是相同的。 – ajmena

回答

0

我终于决定只使用我自己的驱动器帐户。所以我创建了一个令牌,我一直在使用它。我跟着这些主题:

1.-创建谷歌云端硬盘API令牌:https://stackoverflow.com/a/19766913/4965910

2:谷歌API添加到MVC正确,并使用我们的令牌: GoogleWebAuthorizationBroker in MVC For Google Drive Access

你必须小心,很容易陷入一些步骤。

这仅适用于具有一个驱动器帐户的ASP NET MVC应用程序。我知道不是直接解决问题,这种方式避免了它。希望能帮助到你。

+0

嗨 我有同样的问题。你是否曾经为使用oauth的所有用户工作? – user1763470

+0

如果您暗示这是否适用于不同的驱动器帐户,每个用户一个,答案是:我不知道。我没有解决问题,我避免只使用我自己的帐户。 – ajmena

0

该代码在本地计算机上正常工作,但发布版本提供“访问被拒绝”错误。

在将Web应用程序部署到Azure网站后,我遇到同样的问题。

enter image description here

我指的是“Web应用程序(ASP.NET MVC)”从this article节得到用户的凭据和使用DriveService,我觉得它工作正常,在Azure上的网站。