Groovy的"execute"方法和通常的运行bash命令有什么区别?

What is the difference between Groovy's "execute" method and running bash commands normally?

我正在尝试使用 Bitbucket 的 API 从刷新密钥生成新的访问密钥。我可以使用以下方法在终端中成功执行此操作:

curl -X POST -u "${client_id}:${secretKey}" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token=${refreshToken}

其中 ${...} 已被其文字值替换。它正确 returns 类似这样的东西:

{"access_token": "xxxxx", "scopes": "pullrequest", "expires_in": 3600, "refresh_token": "xxxxx", "token_type": "bearer"}

我正在使用 Groovy 执行此操作:

def getAccessToken = "curl -X POST -u \"${client_id}:${secret}\" https://bitbucket.org/site/oauth2/access_token -d grant_type=refresh_token -d refresh_token=${refreshToken}"
def getAccessTokenResult = getAccessToken.execute().text

当我打印命令和 运行 结果时它会起作用,所以它不是格式错误的 URL。当我打印命令本身的结果时,它会返回:

{"error_description": "Invalid OAuth client credentials", "error": "unauthorized_client"}

没有理由会发生这种情况,除非命令的执行方式存在根本差异 运行,如果有人知道差异或什至可以解决此问题,我将不胜感激。

我使用 https://httpbin.org/ 测试了这两种方法,发现服务器返回的 Authorization header 与每种方法不同。

我相信将您的凭据 (-u) 包装在 shell 中的 double-quotes 中只是告诉 shell 它们应该被视为一个参数.它们不会传递给 curl.

看起来 Groovy 将引号作为参数的一部分。删除它们会在 shell 和 Groovy 中生成相同的 Authorization header,但当然现在您可能需要转义客户端 ID 和密钥值中的某些字符:

... -u ${client_id}:${secretKey} ...