Retrofit2调用Instagram API发送POST请求遇400错误求助
Hey there, let's work through this frustrating issue together! That 400 error saying your request is missing client_id or access_token and being treated as a GET request is a common pitfall when setting up Retrofit for POST APIs—here's what's likely going wrong and how to fix it:
Common Causes & Fixes
1. You're Using the Wrong Retrofit Annotation (or Misconfiguring the Request)
First up: double-check your Retrofit interface method. If you accidentally used @GET instead of @POST, or didn't structure the parameters correctly, Instagram's API will treat the request as a GET (which expects auth params in the URL) and throw that error.
Instagram's comment endpoint requires a POST request to https://api.instagram.com/v1/media/{media-id}/comments, with your access_token as a URL query parameter, and the comment text in the request body as JSON.
Here's how your interface should look:
public interface InstagramApiService { // Correct POST annotation with path parameter for media ID @POST("v1/media/{mediaId}/comments") Call<CommentResponse> postComment( @Path("mediaId") String mediaId, @Query("access_token") String accessToken, // Auth param as URL query @Body CommentRequest commentBody // Comment text in request body ); }
2. Your Request Body Isn't Set Up Properly
You need a simple model class to wrap the comment text for the JSON body:
public class CommentRequest { @SerializedName("text") private String commentText; public CommentRequest(String commentText) { this.commentText = commentText; } }
3. Verify Your Retrofit Instance Configuration
Make sure your Retrofit instance is set up with the correct base URL and JSON converter (so it can serialize your CommentRequest into the required JSON body):
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.instagram.com/") .addConverterFactory(GsonConverterFactory.create()) // Critical for JSON handling .build();
4. Double-Check Your Call Logic
When you grab the text from your EditText in the AlertDialog, make sure you're passing all parameters correctly:
// Get comment text from your EditText String commentText = yourEditText.getText().toString().trim(); if (commentText.isEmpty()) { // Handle empty comment case return; } // Initialize the API service InstagramApiService apiService = retrofit.create(InstagramApiService.class); // Make the POST call Call<CommentResponse> call = apiService.postComment( "YOUR_MEDIA_ID_HERE", "YOUR_ACCESS_TOKEN_HERE", new CommentRequest(commentText) ); call.enqueue(new Callback<CommentResponse>() { @Override public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) { if (response.isSuccessful()) { // Comment posted successfully! Toast.makeText(context, "Comment added!", Toast.LENGTH_SHORT).show(); } else { // Log the error body to debug further try { Log.e("InstagramError", "Error: " + response.errorBody().string()); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(Call<CommentResponse> call, Throwable t) { t.printStackTrace(); Toast.makeText(context, "Request failed", Toast.LENGTH_SHORT).show(); } });
Quick Troubleshooting Checks
- Ensure you're not accidentally using
@GETinstead of@POSTin your interface. - Confirm your
access_tokenis being passed as a@Queryparameter (not in the request body or headers, unless Instagram's docs specify otherwise—your error message points to URL parameters). - Make sure there are no interceptors in your Retrofit setup that might be modifying the request method (like a misconfigured logging interceptor).
内容的提问来源于stack exchange,提问作者Lenormand Tom

