繁体   English   中英

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}

$ {...}已被其文字值替换的地方。 它正确返回类似于以下内容的内容:

{"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标头对于每种方法均不同。

我相信,将您的凭据( -u )用双引号括起来在shell中只会告诉shell它们将被视为一个参数。 他们没有通过curl

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM