2016-07-22 48 views
1

我正在尝试使用Bitbucket的API从刷新密钥生成新的访问密钥。我可以成功地在终端中使用:Groovy的“execute”方法和正常运行bash命令有什么区别?

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。当我的println命令本身的结果,它回来了这一点:

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

没有理由这应该发生,除非有怎样的命令正在运行的根本区别,如果有人知道的区别或者甚至可以解决这个问题,我将非常感激。

+1

您使用了大量shell变量中Groovy中的'curl'命令。当你运行Groovy代码时他们是否有价值? (很可能,你需要在运行之前导出它们,否则,实际运行的'curl'命令可能看起来像是'curl -X POST -u“:”https://bitbucket.org/site/oauth2/access_token -d grant_type = refresh_token -d refresh_token =' – chepner

+1

@chepner在这里回答OP - 解析字符串是在执行之前完成的,getAccessToken的'println'显示正确填充所有值的完整'curl'命令;该命令可以粘贴到终端并直接运行(并给出我们期望的结果)。 –

回答

2

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

我相信在你的shell中使用双引号包装你的凭证(-u)只是告诉shell它们被视为一个参数。他们没有传递给curl

看起来Groovy包含引号作为参数的一部分。删除它们产生在外壳和Groovy都相同Authorization头,当然,现在你可能需要逃避您的客户端ID某些字符和秘密键值:

... -u ${client_id}:${secretKey} ... 
相关问题