Groovy的“执行”方法和正常运行的bash命令有什么区别?

时间:2016-07-22 14:02:15

标签: java bash curl groovy bitbucket

我正在尝试使用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"}

没有理由这样做,除非命令运行方式有根本区别,如果有人知道差异或者甚至可以解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:2)

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

我认为将您的凭据(curl)包装在shell中的双引号中只是告诉shell它们应该被视为单个参数。它们不会传递给Authorization

看起来Groovy将引号包含在参数中。删除它们会在shell和Groovy中生成相同的... -u ${client_id}:${secretKey} ... 标头,但当然现在您可能需要转义客户端ID和密钥值中的某些字符:

{{1}}