instagram4j: "400, Something went wrong" while posting photo

Hi,

I’m uploading a photo

instagram.sendRequest(new InstagramUploadVideoRequest(post.getFile(), post.getDescription()));

I’m 100% sure that post.getFile() return a valid FIle and post.getDescription() return a non null String and I’m getting this ugly error

2019-11-19 21:40:32 [scheduling-1] INFO  org.brunocvcunha.instagram4j.Instagram4j  - Sending request: org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest
2019-11-19 21:40:32 [scheduling-1] INFO  org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest  - URL Upload: https://i.instagram.com/api/v1/upload/photo/
2019-11-19 21:40:33 [scheduling-1] INFO  org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest  - Photo Upload result: 400, Something is wrong. Please check your input and try again.
2019-11-19 21:40:33 [scheduling-1] INFO  org.brunocvcunha.instagram4j.requests.InstagramRequest  - Reading InstagramConfigurePhotoResult from Something is wrong. Please check your input and try again.
2019-11-19 21:40:33.521 ERROR 4696 --- [   scheduling-1] a.tassinari.core.InstaService            : Unrecognized token 'Something': was expecting ('true', 'false' or 'null')
 at [Source: (String)"Something is wrong. Please check your input and try again."; line: 1, column: 10]

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Something': was expecting ('true', 'false' or 'null')
 at [Source: (String)"Something is wrong. Please check your input and try again."; line: 1, column: 10]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804) ~[jackson-core-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703) ~[jackson-core-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853) ~[jackson-core-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899) ~[jackson-core-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:757) ~[jackson-core-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141) ~[jackson-databind-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000) ~[jackson-databind-2.9.9.jar:2.9.9]
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) ~[jackson-databind-2.9.9.jar:2.9.9]
	at org.brunocvcunha.instagram4j.requests.InstagramRequest.parseJson(InstagramRequest.java:141) ~[instagram4j-1.11.jar:na]
	at org.brunocvcunha.instagram4j.requests.InstagramRequest.parseJson(InstagramRequest.java:111) ~[instagram4j-1.11.jar:na]
	at org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest.parseResult(InstagramUploadPhotoRequest.java:171) ~[instagram4j-1.11.jar:na]
	at org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest.execute(InstagramUploadPhotoRequest.java:106) ~[instagram4j-1.11.jar:na]
	at org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest.execute(InstagramUploadPhotoRequest.java:44) ~[instagram4j-1.11.jar:na]
	at org.brunocvcunha.instagram4j.Instagram4j.sendRequest(Instagram4j.java:281) ~[instagram4j-1.11.jar:na]
	at a.tassinari.core.InstaService.postPhoto(InstaService.java:67) [classes/:na]
	at a.tassinari.core.InstaService.postFile(InstaService.java:44) [classes/:na]
	at a.tassinari.core.Scheduler.schedule(Scheduler.java:41) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) [spring-context-5.2.0.M3.jar:5.2.0.M3]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.2.0.M3.jar:5.2.0.M3]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.0.M3.jar:5.2.0.M3]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_171]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_171]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_171]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_171]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

I tryed different versions (1.11, 1.12, 1.13) with no results

Can you help me?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 36

Most upvoted comments

Hey all,

Sorry for delay! As I am very busy, it is hard to write, test, and validate code for long periods.

I am happy to say that I’ve decided to improve some of the core functions of the library that were not working. So far, I have gotten:

  • Photo upload to work (including albums and stories!)
  • Video upload to work
  • Timeline feed and pagination (merging my previous PR #355 with new upcoming one)

These are what I believe to be the main core functionalities and I’ve worked to improve the library by making requests use newer headers, and overall improve the library without (hopefully) much breaking changes. The PR will be posted soon once I tidy up my work. You can see progress over at my fork of the repo.

I am grateful of other instagram private api (namely instagram-php and instagram node.js) for doing much of the hard work! Again, once I have proper time, I would like to rewrite the library to be more intuitive for users and be easier to manage and add new features as time goes.

Hi all,

I looked into the issue, and I have successfully fixed the photo upload request. It is using the new endpoint per the php and node.js library. I just have to clean it up a bit, and make sure photo/album/story posting is fully working. I will make a pull request soon (as I am very busy!).

For now, enjoy this gist for a quick fix. https://gist.github.com/jvogit/a98b5e411ab7bc9a33a02d51267ac66c

Some remarks,

  • it has been tested for a single photo upload (not story or album currently as of this post)
  • the ig capabilities you can find them in the php library constants, or elsewhere. (you may also need to update constants overall, but I am planning to include these in my pull request)
  • this library is very much in need of refactoring/rewriting, something I’m planning to do once I have free time off of university.

Thanks @jvogit for finding a solution.

I am not able to put everything together and test at this time, but if you are willing to open a PR, I am more than happy to review and merge it.

Thank @jvogit . I pulled the code from you forked version and it’s working now. Thank you very much for your help.

Still not working for me. Just built the latest 1.14-SNAPSHOT and still getting the Challenge required error and been logged out after calling login. Thanks.

For challenge,

See #358

Ran into this problem while trying to get my social media manager program running again, cant upload image or album of images, can anyone run me through quickly whats needed to be done to fix this or when a fixed version will be out that I can update my program to use the new version?

Just read the reply by jvogit above, I’m sure he will provide a reliable fix as soon as possible for him.

InstagramUploadPhotoRequest:

public class InstagramUploadPhotoRequest extends InstagramRequest<InstagramConfigurePhotoResult> {

private File imageFile;

private String caption;
private String uploadId;

@Override
public String getUrl() {
	return "upload/photo/";
}

@Override
public String getMethod() {
	return "POST";
}

public InstagramUploadPhotoRequest(File imageFile, String caption) throws IOException {
	this(imageFile, caption, null);
}

public InstagramUploadPhotoRequest(File imageFile, String caption, String uploadId) throws IOException {
	this.imageFile = imageFile;
	this.caption = caption;
	this.uploadId = uploadId;
}

@Override
public InstagramConfigurePhotoResult execute() throws ClientProtocolException, IOException {

	if (uploadId == null) {
		uploadId = String.valueOf(System.currentTimeMillis());
	}

	HttpPost post = createHttpRequest();
	post.setEntity(createMultipartEntity());

	try (CloseableHttpResponse response = api.getClient().execute(post)) {
		api.setLastResponse(response);

		int resultCode = response.getStatusLine().getStatusCode();
		String content = EntityUtils.toString(response.getEntity());

		log.info("Photo Upload result: " + resultCode + ", " + content);

		post.releaseConnection();

		StatusResult result = parseResult(resultCode, content);

		if (!result.getStatus().equalsIgnoreCase("ok")) {
			throw new RuntimeException("Error happened in photo upload: " + result.getMessage());
		}


		InstagramConfigurePhotoResult configurePhotoResult = api.sendRequest(new InstagramConfigurePhotoRequest(MyImageUtils.getImage(imageFile), uploadId, caption));

		log.info("Configure photo result: " + configurePhotoResult);
		if (!configurePhotoResult.getStatus().equalsIgnoreCase("ok")) {
			throw new IllegalArgumentException("Failed to configure image: " + configurePhotoResult.getMessage());
		}

		StatusResult exposeResult = api.sendRequest(new InstagramExposeRequest());
		log.info("Expose result: " + exposeResult);
		if (!exposeResult.getStatus().equalsIgnoreCase("ok")) {
			throw new IllegalArgumentException("Failed to expose image: " + exposeResult.getMessage());
		}

		return configurePhotoResult;
	}
}

/**
 * Creates required multipart entity with the image binary
 *
 * @return HttpEntity to send on the post
 * @throws ClientProtocolException
 * @throws IOException
 */
protected HttpEntity createMultipartEntity() throws ClientProtocolException, IOException {
	return new ByteArrayEntity(Files.readAllBytes(imageFile.toPath()));
}

protected String photoUploadParam() {
	final JSONObject jsonObject = new JSONObject();

	jsonObject.put("retry_context", "{ \"num_step_auto_retry\": 0, \"num_reupload\": 0, \"num_step_manual_retry\": 0 }");
	jsonObject.put("media_type", "1");
	jsonObject.put("upload_id", uploadId);
	jsonObject.put("image_compression", "{\"lib_name\":\"moz\",\"lib_version\":\"3.1.m\",\"quality\":\"95\"}");

	return jsonObject.toString();
}

/**
 * Creates the Post Request
 *
 * @return Request
 */
protected HttpPost createHttpRequest() throws IOException {
	final String uploadIdHashCode = String.valueOf(imageFile.getName().hashCode());
	final String EntityName = uploadId + "_0_" + uploadIdHashCode;

	final String url = "https://i.instagram.com/rupload_igphoto/" + EntityName;
	log.info("URL Upload: " + url);

	HttpPost post = new HttpPost(url);
	post.addHeader("X-IG-Capabilities", "3Q4=");
	post.addHeader("X-IG-Connection-Type", "WIFI");
	post.addHeader("X-IG-APP-ID", "567067343352427");
	post.addHeader("X-Entity-Type", "image/jpeg");
	post.addHeader("Offset", "0");
	post.addHeader("X-Instagram-Rupload-Params", photoUploadParam());
	post.addHeader("X-Entity-Name", EntityName);
	post.addHeader("X-Entity-Length", String.valueOf(Files.readAllBytes(imageFile.toPath()).length));
	post.addHeader("X_FB_PHOTO_WATERFALL_ID", UUID.randomUUID().toString());
	post.addHeader("Accept-Language", "en-US");
	post.addHeader("User-Agent", InstagramConstants.USER_AGENT);
	post.addHeader("Content-Transfer-Encoding", "binary");
	post.addHeader("Content-Type", "application/octet-stream");

	return post;
}

@Override
public InstagramConfigurePhotoResult parseResult(int statusCode, String content) {
	return parseJson(statusCode, content, InstagramConfigurePhotoResult.class);
}
}

https://www.instagram.com/developer/ Instagram is moving to a new API. All old libraries are dying. We must either look for another way (Silenum for example) or wait until there are new libraries supporting the new API

I think thats the public api, while these inofficial libraries use the private one by pretending to be an android device. Aside from that, the php & node.js libraries are working fine after they made the change i mentioned above.

https://www.instagram.com/developer/ Instagram is moving to a new API. All old libraries are dying. We must either look for another way (Silenum for example) or wait until there are new libraries supporting the new API

Same here…