curl to retrofit2 post命令

时间:2016-11-12 18:48:54

标签: android curl oauth retrofit

我正在尝试从gfycat api获取访问令牌。在docs之后,我可以使用以下命令在终端中获得一个好的令牌。

curl -v -XPOST -d '{"client_id":"YOUR_ID_HERE", "client_secret": "YOUR_SECRET_HERE", "grant_type": "client_credentials"}' https://api.gfycat.com/v1/oauth/token

但是,在尝试使用我的改装客户端获得相同的结果时,响应正文为空。这是我的代码:

public class GfycatApiManager {
    private static final String BASE_URL = "https://api.gfycat.com/v1/";
    private static final String GRANT_TYPE = "client_credentials";
    private static final String CLIENT_ID = "my id";
    private static final String CLIENT_SECRET = "my secret";

    private GfycatApiInterface api;

    public GfycatApiManager() {
            api = (new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build())
                    .create(GfycatApiInterface.class);
        }

        public  void getToken(){
            Call<AccessToken> call = api.getAccessToken(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);
            call.enqueue(new Callback<AccessToken>() {
                @Override
                public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
                    //System.out.println(response.body().token_type);
                    //System.out.println(response.body().scope);
                    //System.out.println(response.body().expires_in);
                    //System.out.println(response.body().access_token);
                }
                @Override
                public void onFailure(Call<AccessToken> call, Throwable t) {}
            });

        }
}

服务......

public interface GfycatApiInterface {

    @FormUrlEncoded
    @POST("/oauth/token/")
    Call<AccessToken> getAccessToken(@Field("grant_type") String grantType,
                                     @Field("client_id") String clientId,
                                     @Field("client_secret") String clientSecret);
}

令牌......

public class AccessToken {

        @SerializedName("token_type")
        @Expose
        public String token_type;
        @SerializedName("scope")
        @Expose
        public String scope;
        @SerializedName("expires_in")
        @Expose
        public int expires_in;
        @SerializedName("access_token")
        @Expose
        public String access_token;

    }

不确定我的post命令或其他地方是否有问题,但我无法弄明白。请帮忙:D

1 个答案:

答案 0 :(得分:0)

我能够使用@Body注释进行调用。以下更改使其适用于我:

GfycatApiInterface.java - 删除路径中的初始正斜杠并更改签名

public interface GfycatApiInterface {

    @POST("oauth/token/")
    Call<AccessToken> getAccessToken(@Body TokenRequest tokenRequest);
}

TokenRequest.java - 包含请求参数的新模型类

public class TokenRequest {

    private String grant_type;
    private String client_id;
    private String client_secret;
    
    public TokenRequest(String grantType, String clientId, String clientSecret) {
        this.grant_type = grantType;
        this.client_id = clientId;
        this.client_secret = clientSecret;
    }
    
}

GfycatApiManager.java中的getToken()调用应该使用:

Call<AccessToken> call = api.getAccessToken(new TokenRequest(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET));