【问题标题】:Google Cloud SDK cURL cloud not resolve hostGoogle Cloud SDK cURL 云无法解析主机
【发布时间】:2018-10-16 11:53:37
【问题描述】:

我正在尝试在 Windows 上设置 Google Cloud 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

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

curl: (6) cloud not resolve host: auth
curl:(6)云不解析主机:应用程序默认
curl:(6)云无法解析主机:print-access-token

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

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

编辑: 当我原谅

gcloud auth application-default print-acces-token 命令
返回环境变量不存在!但是我执行了 set 命令。我究竟做错了什么 ?

【问题讨论】:

    标签: json curl http-status-code-401 google-cloud-sdk


    【解决方案1】:

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

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

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

    然后您可以将其分配给环境变量:

    set /p token=<token.txt
    

    并尝试像这样运行您的 Text-to-Speech 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
    

    【讨论】:

    • 我无法访问json文件返回环境变量不存在
    • 每次打开新的CMD窗口都需要设置,也可以创建custom environment variable
    • 我每次打开 mcd 窗口时都会设置 json 文件,但是当我尝试访问云时,它返回的环境变量不存在。我设置了我在问题中显示的变量。自定义环境也不起作用...
    • 您是否也已经执行了gcloud init 命令?这是为了使用您的用户帐户凭据授权访问。如果你有时间,你可以加入这个chat讨论你的场景
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2010-11-23
    • 2011-05-20
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多