gfycat api에서 액세스 토큰을 얻으려고합니다. docs 다음 명령을 사용하여 터미널에서 좋은 토큰을 얻을 수 있습니다.curl to retrofit2 게시 명령
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
그러나 내 개조 클라이언트에서 동일한 결과를 얻으려고 할 때 응답 본문은 null입니다. 여기에 내 코드입니다 :
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
시도 private 필드를 만들고'Alt + Insert'를 사용하여 AccessToken''의 모델 클래스의 표준 세터와 게터를 사용하는 –
게터/세터 추가 및 비공개로 설정하면 아무런 차이가 없습니다. response.body() is null –