Google Cloud SDK cURL 云无法解析主机

Google Cloud SDK cURL cloud not resolve host

我正在尝试在 windows 上设置 Google 云 Api。我花了很多时间来解决这个问题阅读所有相关问题没有任何帮助! 首先我设置变量

set GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Desktop\directory\filename.json"

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
   --data "{
    'input':{
    'text':'Android is a mobile operating system developed by Google,
     based on the Linux kernel and designed primarily for
     touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
     'languageCode':'en-gb',
     'name':'en-GB-Standard-A',
     'ssmlGender':'FEMALE'
    },
    'audioConfig':{
     'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1/text:synthesize" > synthesize-text.txt

如 google 文档中所述。但是在命令行中

curl: (6) cloud not resolve host: auth
curl: (6) cloud not resolve host: application-default
curl: (6) cloud not resolve host: print-access-token

告诉我这个错误。在 synthesize-text.txt 中表示错误代码 401。

我该如何解决这个问题。任何帮助表示赞赏。

编辑: 当我原谅

gcloud auth application-default print-acces-token command
Returns环境变量不存在!但是我执行了设置命令。我做错了什么?

这是因为 windows 不接受 $(gcloud auth application-default print-access-token) 用法。

您可以尝试将打印访问令牌的命令输出写入文件。例如:

gcloud auth application-default print-access-token > token.txt

然后你可以将其分配给一个环境变量:

set /p token=<token.txt

然后尝试 运行 您的文本转语音 API 请求,如下所示:

curl -H "Authorization: Bearer "%token% \
  -H "Content-Type: application/json; charset=utf-8" \
   --data "{
    'input':{
    'text':'Android is a mobile operating system developed by Google,
     based on the Linux kernel and designed primarily for
     touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
     'languageCode':'en-gb',
     'name':'en-GB-Standard-A',
     'ssmlGender':'FEMALE'
    },
    'audioConfig':{
     'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1/text:synthesize" > synthesize-text.txt

或者,您可以尝试使用 API 键来满足您的要求。例如:

curl -H "Content-Type: application/json; charset=utf-8" \ 
--data "{ 
'input':{ 
'text':'Android is a mobile operating system developed by Google, 
based on the Linux kernel and designed primarily for 
touchscreen mobile devices such as smartphones and tablets.' 
}, 
'voice':{ 
'languageCode':'en-gb', 
'name':'en-GB-Standard-A', 
'ssmlGender':'FEMALE' 
}, 
'audioConfig':{ 
'audioEncoding':'MP3' 
} 
}" "https://texttospeech.googleapis.com/v1/text:synthesize?key=YOUR_API_KEY" > synthesize-text.txt