【发布时间】:2018-08-23 19:16:22
【问题描述】:
我收到了错误
在 mscorlib.dll 中发生“Google.GoogleApiException”类型的未处理异常附加信息:Google.Apis.Requests.RequestError 调用者没有权限 [403] 错误[消息[调用者没有权限]位置[-]原因[禁止]域[全局]]
或
请求具有无效的身份验证凭据。预期的 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。 见https://developers.google.com/identity/sign-in/web/devconsole-project。 [401]
请让我知道我需要编辑什么
string AccountFilePath = @"C:\GFApps\My App\Google Service\ServiceAccount\Json\CREG Service-427e7b31069c.json";
string AccountEmail = "cregadmin@creg-service.iam.gserviceaccount.com";
string AccountAdminUser = "googleIntegration.NET@domain.com";
string UserToImpersonate = null;
ServiceAccountCredential credential;
var credentialParameters = NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(File.ReadAllText(AccountFilePath));
using (var stream = new FileStream(AccountFilePath, FileMode.Open, FileAccess.Read)) {
credential = ServiceAccountCredential.FromServiceAccountData(stream);
}
var credentialforuser = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(AccountEmail) {
Scopes = Scopes,
User = string.IsNullOrEmpty(UserToImpersonate) ? AccountAdminUser : UserToImpersonate,
Key = credential.Key
} .FromPrivateKey(credentialParameters.PrivateKey));
Console.WriteLine("read from spread sheet ");
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer() {
HttpClientInitializer = credentialforuser,
ApplicationName = ApplicationName,
});
// Define request parameters.
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
// String spreadsheetId = "1nWdSM90mG7qT8YcOucHc_3NdmLYLpZd3FPJOF4lXMhY";
String range = "Class Data!A2:E";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
if (values != null && values.Count > 0) {
Console.WriteLine("Name, Major");
foreach (var row in values) {
// Print columns A and E, which correspond to indices 0 and 4.
Console.WriteLine("{0}, {1}", row[0], row[4]);
}
} else {
Console.WriteLine("No data found.");
}
【问题讨论】:
标签: google-sheets google-api google-oauth google-api-client google-api-dotnet-client