2016-11-12 122 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

難過...謝謝! –