我正在使用C#语言,服务使我异常
“无法通过https://accounts.google.com/o/oauth2/v2/auth启动浏览器”
我遵循的步骤:
我的代码是:
public File InsertFile(byte[] byteArray)
{
// File's metadata
File body = new File();
body.Title = "my title";
body.Description = "my description";
body.MimeType = "image/jpg";
// File's content.
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
var credential = Authentication();
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = string.Format("{0} elfeedback-project", System.Diagnostics.Process.GetCurrentProcess().ProcessName),
});
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "image/jpg");
request.UploadAsync();
File file = request.ResponseBody;
return file;
}
catch (Exception ex)
{
return null;
}
}`
`public UserCredential Authentication()
{
string[] scopes = { DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
};
UserCredential credential;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "my client id",
ClientSecret = "my client secret"
},
scopes,
"myGmail Account that authenticated (client id ,client secret)",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result;
return credential;
}
答案 0 :(得分:0)
您正在使用已安装的应用程序的代码。该代码试图在服务器上而不是用户浏览器上打开新的浏览器窗口以征得同意。
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PUT_CLIENT_ID_HERE",
ClientSecret = "PUT_CLIENT_SECRET_HERE"
},
Scopes = new[] { DriveService.Scope.Drive },
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
如果您想上传到自己控制的帐户,则应考虑使用service account。服务帐户是虚拟用户,他们具有自己的Google云端硬盘帐户,您可以将其上传到其中。您无法使用网络视图查看此帐户的内容。您将需要创建服务帐户凭据而不是OAuth凭据。
public static DriveService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Service account Authentication Sample",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Drive service.
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
throw new Exception("CreateServiceAccountDriveFailed", ex);
}
}
}
从ServiceAccount.cs翻录的代码