我正在尝试使用 Bitbucket 的 API 从刷新 key 生成新的访问 key 。我可以使用以下命令在终端中成功执行此操作:
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
当我 println 命令并运行结果时,它会起作用,因此它不是格式错误的 URL。当我打印命令本身的结果时,它会返回以下内容:
{"error_description": "Invalid OAuth client credentials", "error": "unauthorized_client"}
除非命令的运行方式存在根本差异,否则没有理由发生这种情况,如果有人知道其中的差异或者甚至可以解决这个问题,我将非常感激。
最佳答案
我使用 https://httpbin.org/ 测试了这两种方法并发现服务器返回的 Authorization
header 对于每种方法都不同。
我相信,在 shell 中将凭据 (-u
) 用双引号括起来只是告诉 shell 它们应该被视为单个参数。它们不会传递给 curl
。
看起来 Groovy 包含引号作为参数的一部分。删除它们会在 shell 和 Groovy 中生成相同的 Authorization
header ,但当然现在您可能需要转义客户端 ID 和 key 值中的某些字符:
... -u ${client_id}:${secretKey} ...
关于java - Groovy的 "execute"方法和正常运行bash命令有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528416/