Reputation: 1584
Since Instagram
does not allow to post photo using API end points, I'm sharing a photo on Instagram
app using android intent
from another app. How do I get the media Id of the shared post?
Can we use hidden hashtags
to identify that particular post?
Upvotes: 1
Views: 5309
Reputation: 4267
if you want to get media id from a shareable link, you can go this link. It will response JSON.
http://api.instagram.com/oembed?url=https://instagram.com/p/6GgFE9JKzm/
Response:
{
"provider_url": "https://instagram.com/",
"media_id": "1046665049816739046_489992346",
"author_name": "8crap",
"height": null,
"provider_name": "Instagram",
"title": "Just as the goal in life is to work the least and get paid the most. #8crap",
"html": "<blockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-version=\"4\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\"><div style=\"padding:8px;\"> <div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;\"> <div style=\" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\"></div></div> <p style=\" margin:8px 0 0 0; padding:0 4px;\"> <a href=\"https://instagram.com/p/6GgFE9JKzm/\" style=\" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;\" target=\"_top\">Just as the goal in life is to work the least and get paid the most. #8crap</a></p> <p style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\">8crap (@8crap) tarafından paylaşılan bir fotoğraf (<time style=\" font-family:Arial,sans-serif; font-size:14px; line-height:17px;\" datetime=\"2015-08-08T00:03:38+00:00\">7 Ağu 2015, 17:03 PDT</time>)</p></div></blockquote>\n<script async defer src=\"//platform.instagram.com/en_US/embeds.js\"></script>",
"width": 658,
"version": "1.0",
"author_url": "https://instagram.com/8crap",
"author_id": 489992346,
"type": "rich"
}
So, you will get media_id item.
Code for connection and parsing json:
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String jsonStr = null;
try {
// Construct the URL. "BASED_URL + urlString" should be like this
// "http://api.instagram.com/oembed?url=https://instagram.com/p/6GgFE9JKzm/"
URL url = new URL(BASED_URL + urlString);
// open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
jsonStr = buffer.toString();
} catch (IOException e) {
exception = e;
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
exception = e;
}
}
}
try {
String mediaId = getMediaIdFromJson(jsonStr);
} catch (JSONException e) {
exception = e;
}
private String getMediaIdFromJson(String jsonStr) throws JSONException {
// Parse media id from JSON
if (jsonStr == null) {
return "";
}
final String MEDIA_ID = "media_id";
JSONObject jsonObject = new JSONObject(jsonStr);
String mediaId = jsonObject.getString(MEDIA_ID);
return mediaId;
}
Upvotes: 1