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上的網站。