【发布时间】:2016-06-15 03:00:14
【问题描述】:
错误:解析值时遇到意外字符:e。小路 '',第 0 行,第 0 位。
我正在使用 Google .Net 客户端库来访问 Google 驱动器 API v3,特别是 Google.Apis.Drive.v3 包。我正在通过 C# 授权使用“服务帐户”。
p12 密钥授权没问题。但是,建议使用 JSON,并保留 p12 格式以实现向后兼容性。
我从 Google Developers Console 下载了 JSON 文件并尝试使用以下代码进行授权:
public static Google.Apis.Drive.v3.DriveService AuthenticateServiceAccountJSON(string keyFilePath) {
// check the file exists
if (!File.Exists(keyFilePath)) {
Console.WriteLine("An Error occurred - Key file does not exist");
return null;
}
string[] scopes = new string[] { DriveService.Scope.Drive, // view and manage your files and documents
DriveService.Scope.DriveAppdata, // view and manage its own configuration data
DriveService.Scope.DriveFile, // view and manage files created by this app
DriveService.Scope.DriveMetadataReadonly, // view metadata for files
DriveService.Scope.DriveReadonly, // view files and documents on your drive
DriveService.Scope.DriveScripts }; // modify your app scripts
try {
using (var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read)) {
var credential = GoogleCredential.FromStream(stream);
if (credential.IsCreateScopedRequired) {
credential.CreateScoped(scopes);
}
// Create the service.
Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "MyDrive",
});
return service;
}
} catch (Exception ex) {
Console.WriteLine(ex.InnerException);
return null;
}
}
我在记事本中查看了 JSON 文件,它似乎是加密的。
“ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAicmFkaWFudC1tZXJjdXJ5LTEyMjkwNyIsCiAgIn....”
可以继续使用P12吗?
【问题讨论】:
-
他们都应该亲自工作我使用 p12 文件。这应该有助于github.com/google/google-api-dotnet-client/issues/659
-
谢谢,我打算用p12。如果有人可以谈谈 JSON 文件,或者它应该如何工作,请告诉我。
标签: c# google-api google-drive-api google-api-dotnet-client service-accounts