我正在使用Google Drive API服务访问我的文件并在google驱动器上上传新文件,我已经在Google控制台中设置了新项目,并设置了OATH Consent
和Client ID
。帐户的身份验证对于Web应用程序来说效果很好,但是当我通过IIS
运行该应用程序时,它会抛出Failed to launch browser
。这是我用来验证GoogleDrive Api
的代码。
public static DriveService GetService()
{
try
{
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { DriveService.Scope.Drive }; //View the files in your Google Drive
UserCredential credential;
var clientSecretjSon = Environment.CurrentDirectory + @"\client_secret.json";
using (var stream = new FileStream(clientSecretjSon, FileMode.Open, FileAccess.Read))
{
String FolderPath = @"E:\";
String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
// Requesting Authentication or loading previously stored authentication for userName
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
// Create Drive API service.
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveDataManagementApp"
});
}
catch (Exception ex)
{
Console.WriteLine("Create Oauth2 account DriveService failed" + ex.Message);
throw new Exception("CreateServiceAccountDriveFailed", ex);
}
}
任何人都可以告诉/建议我如何从IIS托管的网站加载身份验证。