我试图从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
尝试让私营领域和使用模型类的'AccessToken'标准的getter和setter方法使用'ALT + Insert' –
添加getters/setters和私人制作没有区别。 response.body()为null –