2016-07-22 36 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} ... 
相關問題