我正在尝试在Windows(我自己的计算机(上使用Google Translate API。我的默认凭据有问题。错误:**translate.NewClient:拨号:google:找不到默认凭据。
我在谷歌云上有足够的余额。
我开始翻译API。(API启用(
我添加了$env:GOOGLE_APPLICATION_CREDENTIALS=";KEY_PATH">
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/storage"
"cloud.google.com/go/translate"
"golang.org/x/text/language"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func translateTextWithModel(targetLanguage, text, model string) (string, error) {
// targetLanguage := "ja"
// text := "The Go Gopher is cute"
// model := "nmt"
ctx := context.Background()
lang, err := language.Parse(targetLanguage)
if err != nil {
return "", fmt.Errorf("language.Parse: %v", err)
}
client, err := translate.NewClient(ctx)
if err != nil {
return "", fmt.Errorf("translate.NewClient: %v", err)
}
defer client.Close()
resp, err := client.Translate(ctx, []string{text}, lang, &translate.Options{
Model: model, // Either "nmt" or "base".
})
if err != nil {
return "", fmt.Errorf("Translate: %v", err)
}
if len(resp) == 0 {
return "", nil
}
return resp[0].Text, nil
}
func main() {
Json_path := "C:/Users/Mels/Documents/GoogleTools/cred-9dfos6bb49f.json"
ProjectID := "cred"
fmt.Println("RUNNING...")
explicit(Json_path, ProjectID)
fmt.Println(translateTextWithModel("ja", "Hello World", "nmt"))
}
// explicit reads credentials from the specified path.
func explicit(jsonPath, projectID string) {
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsFile(jsonPath))
if err != nil {
log.Fatal(err)
}
defer client.Close()
fmt.Println("Buckets:")
it := client.Buckets(ctx, projectID)
for {
battrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(battrs.Name)
}
}
JSON文件
{
"type": "service_account",
"project_id": "xxxxxxx",
"private_key_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"private_key": "-----BEGIN PRIVATE KEY-----XXXXXXXX-----END PRIVATE KEY-----n",
"client_email": "xxxxxx@xxxxxx.iam.gserviceaccount.com",
"client_id": "11111111111",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxxxx%xxxxxxx.iam.gserviceaccount.com"
}
如果库找不到默认凭据,则可以尝试使用凭据路径创建客户端。
即使这对您来说可能不是最好的选择(尽管我更喜欢它而不是env变量(,它也会帮助您更好地诊断问题。
为了创建具有凭据文件路径的客户端,需要导入google.golang.org/api/option
,然后使用WithCredentialsFile
选项创建客户端。请注意,在文档中,可以使用其他选项创建客户端:
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error)
关于如何创建具有选项的客户端,一个更完整的示例如下(根据需要将所需的部分应用于当前代码(:
package main
import (
"cloud.google.com/go/translate"
"context"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
client, err := translate.NewClient(ctx, option.WithCredentialsFile("/path/to/your/file"))
if err != nil {
// TODO: handle error.
}
// Use the client.
// Close the client when finished.
if err := client.Close(); err != nil {
// TODO: handle error.
}
}
(这只是包含附加选项的文档中示例的副本。(
我解决了这个问题。我下载了";谷歌云外壳SDK";并且我使用了";gcloud auth应用程序默认登录";密码SDK提供了一个JSON文件,我用新的JSON文件替换了它。
我不推荐cloud.google.com/translate/docs/setup说明。直接使用谷歌云SDK。