我正在执行这样的授权:
ClientSecrets secrets;
using (var stream = new FileStream(keyFile, FileMode.Open))
{
secrets = GoogleClientSecrets.Load(stream).Secrets;
}
var scopes = new[]
{
AnalyticsService.Scope.AnalyticsReadonly,
WebmastersService.Scope.WebmastersReadonly,
};
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
secrets,
scopes,
AppSettings.Instance.UserName,
CancellationToken.None).Result;
var service = new WebmastersService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "MyApp",
});
var sites = service.Sites.List().Execute();
在此之后,我可以成功创建一个WebmastersService实例;但是,所有操作都会因“权限不足”错误而失败。 Google Search Console API已在https://console.cloud.google.com/apis/enabled网站中启用。我可以使用相同的凭据成功访问Google AnalyticsAPI 我有什么想法吗?
答案 0 :(得分:0)
时间过去了,但对某人来说可能仍然有用......:)
我使用X509Certificate类来加载我的密钥文件,它就像一个魅力。
ConnectionEmail = "aaaaaa@aaaaaaaaaaaaaa.iam.gserviceaccount.com";
ConnectionPassword = "notasecret";
ConnectionKey = File.ReadAllBytes("keyfile.p12");
Scopes = new[]
{
WebmastersService.Scope.Webmasters
};
X509Certificate2 certificate = new X509Certificate2(ConnectionKeyBytes, ConnectionPassword, X509KeyStorageFlags.Exportable);
Service = new WebmastersService(new BaseClientService.Initializer()
{
HttpClientInitializer = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ConnectionEmail) { Scopes = Scopes, }.FromCertificate(certificate)
)
});