新改造,並試圖通過改進來使用Instagram API。以下是設置。 首先我定義的接口與Instagram的APIRetrofit + Instagram API在請求訪問令牌時返回錯誤400
public interface API {
@POST("oauth/access_token")
Call<AccessToken> getAccessToken(@Body TokenRequest tokenRequest);
}
下面的交流是我實現
public class APIPresenter implements Callback<List<APIResponse>> {
static final String BASE_URL = "https://api.instagram.com/";
public void startI(String code){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
API r = retrofit.create(API.class);
Call<AccessToken> call = r.getAccessToken(new TokenRequest("CLIENT_ID", "CLIENT_SECRET", "authorization_code", "redirecturi", code));
call.enqueue(new Callback<AccessToken>() {
@Override
public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
// Log.i("WORKING", response.body().access_token + " " + response.body().user.getUsername());
Log.i("WORKING", String.valueOf(response.code()));
}
@Override
public void onFailure(Call<AccessToken> call, Throwable t) {
t.printStackTrace();
}
});
}
TokenRequest
public class TokenRequest {
private String client_id;
private String client_secret;
private String grant_type;
private String redirect_uri;
private String code;
public TokenRequest(String client_id, String client_secret, String grant_type, String redirect_uri, String code) {
this.client_id = client_id;
this.client_secret = client_secret;
this.grant_type = grant_type;
this.redirect_uri = redirect_uri;
this.code = code;
}
}
的accessToken模型
public class AccessToken {
@SerializedName("access_token")
@Expose
public String access_token;
@SerializedName("user")
@Expose
public User user;
}
問題是當我嘗試在onResponse方法中輸出響應的內容時,我得到一個空值。我不知道我在做什麼錯
更新 我收到以下錯誤消息
{ 「代碼」:400, 「ERROR_TYPE」: 「OAuthException」,「ERROR_MESSAGE 「:」您必須提供一個client_id「}
謝謝,但這種解決方案不會與我的項目一起工作,因爲它是在改進版1.6.1上構建的。然而,我已經解決了這個問題,你可以查看我的答案。 –