2015-11-07 102 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

非常感谢你! –