微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 .NET 中使用 Google.Apis.Translate.v3beta1 翻译 PPT 文件 - 错误 403:“提供的范围未授权”

如何解决在 .NET 中使用 Google.Apis.Translate.v3beta1 翻译 PPT 文件 - 错误 403:“提供的范围未授权”

我一直在尝试使用 Google.Apis.Translate.v3beta1 的 .NET API 来翻译 PPTX 或 DOCX 文件,同时保留文档格式。理论上,根据 https://cloud.google.com/translate/docs/advanced/translate-documents#translate_documents_online 中的描述,这是可能的,但文档太差,无法猜测这应该如何工作。

我希望有人知道这个 API 应该如何工作。

这是我试过的。

  • 创建了一个启用 Cloud Translation API 的 Google Cloud 项目。
  • 创建了一个服务帐户并下载了私钥。
  • 创建了一个存储桶并上传一个文件 AZ01.ppt 进行翻译。
  • 确保服务帐户可以访问 Translation API。我这样做了 Google.Cloud.Translation.V2 和我可以在没有 问题。

正如文档所暗示的,这是我在 C# 中的代码

public async Task doitAsync()
{
    string keyFile = @"mykeyfile.p12";

    // Get the certificate 
    var certificate = new X509Certificate2(keyFile,"notasecret",X509KeyStorageFlags.Exportable);

    // Create the credential so we can generate an access token
    ServiceAccountCredential credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer("mySAErviceaccount@myaccount.iam.gserviceaccount.com")
       {
           Scopes = new[] { Google.Apis.Translate.v3beta1.TranslateService.Scope.CloudTranslation },ProjectId = "myproject",KeyId = "some long key like 3aedc7cfe17d2ad83813f162f4af50597bade165d619d65eab6b5"

       }.FromCertificate(certificate));

    // Get a request token 
    bool success = await credential.RequestAccesstokenAsync(new CancellationToken());
    string token = credential.Token.Accesstoken;

    string json = File.ReadAllText("params.json");

    Google.Apis.Translate.v3beta1.TranslateService serv = new Google.Apis.Translate.v3beta1.TranslateService();
    var client = serv.HttpClient;
    client.BaseAddress = new Uri("https://translation.googleapis.com/v3beta1/projects");
    var httpContent = new StringContent(json,Encoding.UTF8,"application/json");

    var response = await client.PostAsync(
        "https://translation.googleapis.com/v3beta1/projects/myproject/locations/global:translateDocument?access_token=" + token,httpContent);

    if (response.IsSuccessstatusCode)
    {
        string res = await response.Content.ReadAsstringAsync();
        Console.WriteLine("Success: " + res);
    }
    else
    {
        string res = await response.Content.ReadAsstringAsync();
        Console.WriteLine("Failure: " + res);
    }
}

执行这段代码的结果总是:

Failure: {
  "error": {
    "code": 403,"message": "Provided scope(s) are not authorized","errors": [
      {
        "message": "Provided scope(s) are not authorized","domain": "global","reason": "forbidden"
      }
    ],"status": "PERMISSION_DENIED"
  }
}

但是,我的服务帐户与 Translation API 相关联。

所以我希望有人对 Google.Apis.Translate.v3beta1 的 .NET API 有一些经验,并且可以阐明应该如何调用它,或者可以指出我所缺少的.

解决方法

因此,如果您将其添加为作用域,它将起作用:

Scopes = new[] { 
    Google.Apis.Translate.v3beta1.TranslateService.Scope.CloudTranslation,"https://www.googleapis.com/auth/cloud-platform"
},

感谢 Ricco 的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。