2014-11-22 53 views
0

我使用改裝打造的Android應用程序Github上,但我從https://api.github.com/authorizations改造-u用戶名:密碼字段和範圍陣列

我知道如何用命令捲曲的要求去做有麻煩檢索令牌:

curl -X POST -H "Accept: Application/json" -u "username:password" -d '{"scopes": ["user","repo","gist","notifications","repo","repo:status"],"note":"GithubViewer Android App","client_id":"xxx","client_secret":"yyyy"}' "https://api.github.com/authorizations" 

我遇到了麻煩,知道如何設置Scopes數組和-u認證字段。我一直在使用「:基本用戶名:密碼驗證」在命令行中嘗試,但不工作,要麼:(

這裏是什麼,我試圖做一個樣本:

RequestInterceptor interceptor = new RequestInterceptor() { 
     @Override 
     public void intercept(RequestFacade request) { 
        request.addHeader("Authentication", "Basic " + username + ":" + password); 
      } 
     }; 

RestAdapter restAdapter = new RestAdapter.Builder() 
     .setEndpoint(GithubClient.API_URL) 
      .setRequestInterceptor(interceptor) 
      .build(); 

任何建議?

謝謝

+0

你可以發佈你做了什麼嗎? – Atieh 2015-02-02 00:14:42

+0

這就是我所做的。首先,我修改了我的請求,以發送帶請求的@Body。我創建了一個可以序列化爲該請求所需的類的類。 https://gist.github.com/fnk0/f3ef3d5d15d72bff3fb2 – 2015-02-02 19:57:19

+0

我想通了昨天該怎麼做。我錯過了那裏的@Body註釋,不得不創建一個基本的認證。謝謝!你完成了你的應用程序?我可以查看它8D – Atieh 2015-02-02 20:12:37

回答

1

我沒有使用過改造,但我察覺的東西,可能會導致您的問題,您提供的是用戶名:。密碼作爲驗證頭純文本,然而應編碼使用Base64編碼。

String str = "username:password"; 
String base64EncodedUsernamePassword; 

try { 
    base64EncodedUsernamePassword = Base64.encode(str.getBytes("UTF-8"), Base64.NO_WRAP); 
} catch (UnsupportedEncodingException e) { 
    // Weird, no UTF-8 encoding found? 
} 
+0

幫助,謝謝!我想到了最後我的問題......我試圖發送UrlFormEncoded數據到github api,他們只接受post請求中的普通json對象。 – 2014-11-24 16:57:58