2015-11-07 67 views
1

我在Android中使用Retrofit(而不是新建)來執行一些OAuth授權。 我過去的第一步,我得到一個代碼來使用,目前在規範接下來的事情就是要做到這一點:CURL -u請求和更新

curl -u : http://example.com/admin/oauth/token -d 'grant_type=authorization_code&code=' 

我從來沒有做過捲曲的請求,我不知道該怎麼辦,尤其是在改造及其界面方面。

我接過一看這

How to make a CURL request with retrofit?

不過這傢伙可是沒有一個-d事情

回答

2

-d參數捲曲增加POST參數。在這種情況下,grant_typecode。我們可以使用@Field註釋對每個進行改進編碼。

public interface AuthApi { 
    @FormUrlEncoded 
    @POST("/admin/oauth/token") 
    void getImage(@Header("Authorization") String authorization, 
         @Field(("grant_type"))String grantType, 
         @Field("code") String code, 
         Callback<Object> callback); 
} 

如果授權字段正在使用@Header解決方案給你的鏈接問題。

使用會是什麼樣 -

RestAdapter authAdapter = new RestAdapter.Builder().setEndpoint("http://example.com/").build(); 
AuthApi authApi = authAdapter.create(AuthApi.class); 

try { 
    final String auth = "Basic " + getBase64String(":"); 
    authApi.getImage(auth, "authorization_code", "", new Callback<Object>() { 
     @Override 
     public void success(Object o, Response response) { 
      // handle success 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      // handle failure 
     } 
    }); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 

其中getBase64String是從你的鏈接答案的輔助方法。複製下面的完整性 -

public static String getBase64String(String value) throws UnsupportedEncodingException { 
    return Base64.encodeToString(value.getBytes("UTF-8"), Base64.NO_WRAP); 
} 
+0

非常感謝你! –