【发布时间】:2017-03-26 18:33:06
【问题描述】:
我正在尝试从 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
【问题讨论】:
-
尝试使用
Alt+Insert将字段设为私有并在AccessToken模型类中使用标准设置器和获取器 -
添加 getter/setter 并设为私有没有任何区别。 response.body() 为空
标签: android curl oauth retrofit