我一直在遇到一个问题,最近几天,我似乎不是唯一的问题 .

我正在尝试通过Google通过NuGet提供的.NET库连接到Google网站管理员工具 . 我已经在Google Developer Console中成功创建了一个项目,生成了我的服务帐户并检索了一个p12密钥 .

接下来是获取网站站长工具中所有网站的列表,我通过以下方式进行操作:

...
var result = webmasterService.Sites.List().Execute();

运行代码时,我收到以下错误:

Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""

现在问题似乎是我的客户无权访问网站管理员工具资源 . 最近几天我一直在浏览互联网寻求帮助,但我一直坚持这个问题 .

我在哪里必须指定此用户的电子邮件,以便对其进行正确的身份验证?

我的连接器代码是指定的:

string keyPath = Server.MapPath("/keys/webmastertoolsapi.p12");
const string serviceAccountEmail = "yadayada-123123@developer.gserviceaccount.com";
const string appName = "WebMasterToolsAPI";

var certificate = new X509Certificate2(keyPath,"notasecret",X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

var credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        User = "name@domain.com", //email-account which has owner-rights in Webmaster Tools
        Scopes = new[] { WebmastersService.Scope.Webmasters }
    }.FromCertificate(certificate));

var webmasterService = new WebmastersService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = appName,
});