2016-11-12 127 views
0

我试图从gfycat api获取一个访问令牌。在他们的docs之后,我可以通过以下命令在终端中获得一个很好的令牌。curl to retrofit2 post命令

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; 

    } 

不知道它在我的后命令或其他地方有问题但我无法弄清楚。请帮助:D

+0

尝试让私营领域和使用模型类的'AccessToken'标准的getter和setter方法使用'ALT + Insert' –

+0

添加getters/setters和私人制作没有区别。 response.body()为null –

回答

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)); 
+0

像魅力一样工作!任何想法,为什么我的方式不会工作? –

+0

POST正文的格式是问题 - 您将它作为表单参数(如Name = John + Smith&Grade = 19)发送,而文档中的示例使用JSON发送正文数据。 – rhari

+0

难过...谢谢! –