请求访问令牌时,Retrofit + Instagram API返回错误400

时间:2017-02-12 21:51:54

标签: java android retrofit

改造的新手,我正在尝试使用改造来使用Instagram API。以下是设置。 首先,我定义了用于与Instagram API通信的接口

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方法中输出响应的内容时,我得到一个null。我不确定我做错了什么

更新 我收到以下错误消息

  

{&#34;代码&#34;:400,&#34; error_type&#34;:&#34; OAuthException&#34;,&#34; error_message&#34;:&#34;您必须提供CLIENT_ID&#34;}

2 个答案:

答案 0 :(得分:1)

我设法解决了这个问题,问题是Instagram不支持将参数作为URL参数发送,它们必须在POST的正文内容中发送。 内容类型应设置为&#34; application / x-www-form-urlencoded&#34;。

这是您使用上面的代码完成它的方式

您必须在POST方法之前添加@FormURLEncoded注释,这只会对@Field注释起作用,因此@Body内容将替换为以下@Field注释。

public interface API {
@FormURLEncoded
@POST("oauth/access_token")
Call<AccessToken> getAccessToken(
        @Field("client_id") String client_id,
        @Field("client_secret") String client_secret,
        @Field("grant_type") String grant_type,
        @Field("redirect_uri") String redirect_uri,
        @Field("code") String code);
}

这是您将详细信息发送到接口

上的getAccessToken方法的方法
 Call<AccessToken> call = r.getAccessToken("CLIENT_ID", "CLIENT_SECRET", "authorization_code", "redirecturi", code);

这应该完美无缺!

答案 1 :(得分:0)

您以错误的方式发布client_id和其他字段。看看它是如何在类似项目中实现的:

public static String requestOAuthUrl(final String clientId, final String redirectUri, final Scope... scopes) throws URISyntaxException {
        final StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append("response_type=").append("token");
        urlBuilder.append("&client_id=").append(clientId);
        urlBuilder.append("&redirect_uri=").append(redirectUri);
        if (scopes != null) {
            final StringBuilder scopeBuilder = new StringBuilder();
            for (int i = 0; i < scopes.length; i++) {
                scopeBuilder.append(scopes[i]);
                if (i < scopes.length - 1) {
                    scopeBuilder.append(' ');
                }
            }
            urlBuilder.append("&scope=").append(scopeBuilder.toString());
        }
        return new URI("https", "instagram.com", "/oauth/authorize", urlBuilder.toString(), null).toString();
    }

取自https://github.com/wearemakery/retrogram