How can I bypass bot blocking at Instagram to get the full page with file_get_contents() - instagram

I am trying to scrape data from Instagram (IG) user page to use as stream feed at a website, but apparently IG uses a bot blocker so the PHP method does not return all the elements of the JSON string.
The data I am trying to fetch is the following, specifically the "ProfilePage":[{}] object which is visible via local browser when not logged into IG.
<script type="text/javascript">window._sharedData = {
"config": {
"csrf_token": "MaYaasysNl6fy19YthOGUNVOXvYitbmO",
"viewer": null,
"viewerId": null
},
"country_code": "US",
"language_code": "en",
"locale": "en_US",
"entry_data": {
"ProfilePage": [{
"logging_page_id": "profilePage_28995773",
"show_suggested_profiles": true,
"show_follow_dialog": false,
"graphql": {
"user": {
//[...more data follows including images and links]
}
}
</script>
But what I get when scraped via php script is the following which does not include the ProfilePage element and data within the entry_data object.
<script type="text/javascript">window._sharedData = {
"config": {
"csrf_token": "VgzhYPHc6dXWP3qQFKjlWYEBK59bDo6A",
"viewer": null,
"viewerId": null
},
"country_code": "US",
"language_code": "en",
"locale": "en_US",
"entry_data": {
"LoginAndSignupPage": [{
"captcha": {
"enabled": false,
"key": ""
},
"gdpr_required": false,
"tos_version": "row",
"username_hint": ""
}]
},
"hostname": "www.instagram.com",
"is_whitelisted_crawl_bot": false,
"connection_quality_rating": "EXCELLENT",
"deployment_stage": "c2",
"platform": "web",
"nonce": "kQ3AWksy1DQ9TXuwLdKMkw==",
"mid_pct": 84.93332,
"zero_data": {},
"cache_schema_version": 3,
"server_checks": {},
</script>
I have tried using cURL, file_get_contents(), new DOMDocument()->loadHTMLFile() to get all the page source, then preg_match for the line needed, but neither method could bypass the blocker.
$ig = file_get_contents($url);
preg_match_all('#shortcode":"(.*)",#i',$ig, $match);
Expected return
Array
(
[0] => CB-W_D2h3Sz
[1] => B65TtGYBH8Z
[2] => B65SNqsha6O
[3] => B62NvlShguS
[4] => B6zwW5CBETr
[5] => B6wbz5nB_dP
[6] => B6wa3rxBOOP
[7] => B6uw9RwBSSd
[8] => B6tg9dIh_XB
[9] => B6tYekYhIe8
[10] => B6pR-pGhvoE
[11] => B6pRhVgBwaZ
)
Literal return
Array
(
)
How can I get the desired data?

I defined a solution
https://instagram.com/graphql/query/?query_id=17888483320059182&variables={"id":"217723373","first":8,"after":null}

Related

Microsoft Teams task module with URL not working for external url

trigger a task module that will display a web page. All I was able to get is an empty Task Module with the title, while the specified height and width do not showing URL displayed.
i want to redirect the url from the api.,but api is giving a url but not redirecting
let requestUrl = await getRedirectUrlForSubmitAction(tokenResponse) ===> api call to get the url
const response: MessagingExtensionActionResponse = <MessagingExtensionActionResponse>{
task: {
type: "continue",
value: {
title: "Send recognition",
url: `${request.data.value}`, //url from api call
height: "large"
}
}
};
return Promise.resolve(response);
please check this below code for external url redirecting
<html>
<head>
<title>Redirecting</title>
<script src='https://statics.teams.cdn.office.net/sdk/v1.6.0/js/MicrosoftTeams.min.js'></script>
</head>
<body>
<div id='app'>
<header style="display: flex; justify-content: center;align-items: center; font-size: 1rem;">
<h1>Redirecting <em>....</em></h1>
</header>
</div>
<script type="text/javascript">
function login() {
microsoftTeams.initialize();
if (window.location.href.includes("redirectUrl.action")) {
let token = localStorage.getItem("appToken");
let urlStr = window.location.href.split('?url=')[1]
fetch(`${urlStr}`, {
method: 'GET',
headers: new Headers({
"content-type": "application/json",
"originated": "teams",
"post-type": "ajax",
"outlookauth": `${token}`
}),
})
.then(res => res.json())
.then(
(result) => {
console.log("result", result);
location.href = result.url
//return result.url
},
(error) => {
console.log("Error", error);
}
)
} else {
//balance
const host = window.location.href.split('&host=')[1].split('&')[0]
const appcode = window.location.href.split('&appCode=')[1]
const token = localStorage.getItem("appToken");
const urlType = window.location.href.split('?url=')[1]
const urlStr = `https://${host}/${appcode}/mobileapp/teams/teamsShopRedirectUrl.action?${urlType}`
fetch(`${urlStr}`, {
method: 'GET',
headers: new Headers({
"content-type": "application/json",
"originated": "teams",
"post-type": "ajax",
"outlookauth": `${token}`
}),
})
.then(res => res.json())
.then(
(result) => {
window.location.href = result.url
},
(error) => {
console.log("Error", error);
}
)
}
}
window.onload = login();
</script>
</body>
</html>
manifest file
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.8/MicrosoftTeams.schema.json",
"manifestVersion": "1.8",
"id": "{{APPLICATION_ID}}",
"version": "{{VERSION}}",
"packageName": "{{PACKAGE_NAME}}",
"developer": {
"name": "name",
"websiteUrl": "https://{{HOSTNAME}}/now",
"privacyUrl": "https://{{HOSTNAME}}/now/privacy.html",
"termsOfUseUrl": "https://{{HOSTNAME}}/now/tou.html"
},
"name": {
"short": "Now",
"full": "Now"
},
"description": {
"short": "for Teams",
"full": "."
},
"icons": {
"outline": "icon-outline.png",
"color": "icon-color.png"
},
"accentColor": "#FFFFFF",
"configurableTabs": [],
"staticTabs": [],
"bots": [
{
"botId": "{{MICROSOFT_APP_ID}}",
"needsChannelSelector": true,
"isNotificationOnly": false,
"scopes": [
"team",
"personal",
"groupchat"
],
"commandLists": [
{
"scopes": [
"team",
"personal"
],
"commands": [
{
"title": "test1",
"description": "test1"
},
{
"title": "test2",
"description": "test2 "
}
]
}
],
"supportsFiles": true,
"supportsCalling": true,
"supportsVideo": true
}
],
"connectors": [],
"composeExtensions": [
{
"botId": "{{MICROSOFT_APP_ID}}",
"canUpdateConfiguration": true,
"messageHandlers": [
{
"type": "link",
"value": {
"domains": [
"{{HOSTNAME}}",
"avidanpprd.performnet.com",
"youtube.com"
]
}
}
],
"commands": [
{
"id": "MessageExtension",
"title": "title",
"description": "Add a clever description here",
"initialRun": true,
"type": "action",
"context": [
"compose"
],
"fetchTask": true
}
]
}
],
"permissions": [
"identity",
"messageTeamMembers"
],
"validDomains": [
"{{HOSTNAME}}",
"avidanpprd.performnet.com",
"youtube.com"
],
"showLoadingIndicator": true,
"isFullScreen": false
}
This is probably a combination of things:
You need to make sure that the website you're showing is listed as a 'safe' domain in your manifest for your Teams app. I think you need something in both the messageHandlers > value > domain section as well as the validDomains section in the root level of the manifest.
However, depending on what you're trying to embed, it might not work because the external site has to have some basic Teams integration. See this heading, where it says:
For your page to display in Teams, you must include the Microsoft Teams JavaScript client SDK and include a call to microsoftTeams.initialize() after your page loads.
As a result, if this is an external site you don't control, you might need to have your own page that you host in the task module, which simpyly iframes in the external website.
This issue might come if the URL doesn't support iframe embedding.
To help you here you can refer the sample and try to bind the URL in iframe like below in your page.
<iframe width="700" height="700" src="https://www.example.com/embed/QPSaLnaU" allow="autoplay; encrypted-media"></iframe>
I have been recently working on something similar.
I have a message extension (build using Adaptive cards) and on one of the button clicks, I want to trigger Upload Document functionality. Unfortunately, an adaptive card doesn't provide.
So, I build an separate angular web-page, which will act as an upload application.
Note: The domain of the webpage should be added correctly under validDomains in the manifest file.
"validDomains": [
"*.example.com",
],
On button click in message extension (NodeJS):
...
return {
task: {
type: 'continue',
value: {
height: 400,
width: 400,
title: 'Task module WebView',
url: `https://show.example.com`
}
}
};
For more info visit:
Upload attachment from messages extension in MS Teams

elasticsearch node.js API remove an object from an array on a document using painless script results in array Index Out of Bounds

I want to remove items (an object) from an array on a document in elasticsearch, however whenever I try and run my update script using painless, I receive an Array Index Out of Bounds exception.
I'm using the javascript elasticsearch npm package to search elasticsearch for the relevant documents which then returns me data like:
"_index": "centres",
"_type": "doc",
"_id": "51bc77d1-b514-4f4e-85fa-412def6829f5",
"_score": 1,
"_source": {
"id": "cbaa7daa-f1a2-4ac3-8d7c-fc981245d21c",
"name": "Five House",
"openDays": [
{
"title": "new open Day",
"endDate": "2022-03-22T00:00:00.000Z",
"id": "82be934b-eeb1-419c-96ed-a58808b30df7"
},
{
"title": "last open Day",
"endDate": "2020-12-24T00:00:00.000Z",
"id": "8cc339b9-d2f8-4252-b68a-ed0a49cbfabd"
}
]
}
I then want to go through and remove certain items from the openDays array. I've created an array of the items I want to remove, so for the above example:
[
{
id: '51bc77d1-b514-4f4e-85fa-412def6829f5',
indexes: [
{
"title": "last open Day",
"endDate": "2020-12-24T00:00:00.000Z",
"id": "8cc339b9-d2f8-4252-b68a-ed0a49cbfabd"
}
]
}
]
I'm then trying to run an update via the elasticsearch node client like this:
for (const centre of updates) {
if (centre.indexes.length) {
await Promise.all(centre.indexes.map(async (theIndex) => {
const updated = await client.update({
index: 'centres',
type: 'doc',
id: centre.id,
body: {
script: {
lang: 'painless',
source: "ctx._source.openDays.remove(ctx._source.openDays.indexOf('openDayID'))",
params: {
"openDayID": theIndex.id
}
}
}
}).catch((err) => {throw err;});
}))
.catch((err) => {throw err;});
await client.indices.refresh({ index: 'centres' }).catch((err) => { throw err;});
}
}
When I run this though, it returns a 400 with an "array_index_out_of_bounds_exception" error:
-> POST http://localhost:9200/centres/doc/51bc77d1-b514-4f4e-85fa-412def6829f5/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.openDays.remove(ctx._source.openDays.indexOf(\u0027openDayID\u0027))",
"params": {
"openDayID": "8cc339b9-d2f8-4252-b68a-ed0a49cbfabd"
}
}
}
<- 400
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[oSsa7mn][172.17.0.2:9300][indices:data/write/update[s]]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [],
"script": "ctx._source.openDays.remove(ctx._source.openDays.indexOf(\u0027openDayID\u0027))",
"lang": "painless",
"caused_by": {
"type": "array_index_out_of_bounds_exception",
"reason": null
}
}
},
"status": 400
}
I'm not quite sure where I'm going wrong with this. Am I using the indexOf painless script correctly? Does indexOf allow for the searching of properties on objects in arrays?
I stumbled across this question and answer: Elasticsearch: Get object index with Painless script
The body of the update script needs changing like so:
Promise.all(...
const inline = `
def openDayID = '${theIndex.id}';
def openDays = ctx._source.openDays;
def openDayIndex = -1;
for (int i = 0; i < openDays.length; i++)
{
if (openDays[i].id == openDayID)
{
openDayIndex = i;
}
}
if (openDayIndex != -1) {
ctx._source.openDays.remove(openDayIndex);
}
`;
const updated = await client.update({
index: 'centres',
type: 'doc',
id: centre.id,
body: {
script: {
lang: 'painless',
inline: inline,
},
}
}).catch((err) => {throw err;});
await client.indices.refresh({ index: 'centres' }).catch((err) => { throw err;});
})).catch(... //end of Promise.all
I am not au fait with painless scripting, so there are most likely better ways of writing this e.g. breaking once the index of the ID is found.
I have also had to move the refresh statement into the Promise.all since if you're trying to remove more than one item from the array of objects, you'll be changing the document and changing the index. There is probably a better way of dealing with this too.
'openDayID' should be params.openDayID
And use removeIf:
"ctx._source.openDays.removeIf(el -> (el.id == params.openDayID))"

How to show data in React Table with structure {_id:" xx",timestamp:"xx" ,message:"{"temperature:22","humi":45}" }?

React-Table
I have made an axios.get request to the back-end which in turn gives a large data-set from mongodb. The
structure of data returned is :
[
1: {_id: "5dd3be2ecf55e1ec388f502b", timestamp: 1574157870567, message: "{"temperature":58,"humidity":59,"pressure":"1 bar"}"}
2: {_id: "5dd3be2ecf55e1ec388f502a", timestamp: 1574157870067, message: "{"temperature":78,"humidity":79,"pressure":"1 bar"}"}
...
]
I want to show it to react-table.The id and timestamp is being displayed but the temperature and other variable are not being displayed.The message is string.How can I parse such amount of data at back-end to convert message into object?
Back-end code
router.get('/viewData',async(req,res) =>{
collection.find({},{_id:0,timestamp:0}).sort({timestamp:-1}).limit(400).toArray(function (err, resultantData) {
if (err)
throw err;
//var storedDataArray ;
//var gotData=[];
//var index =0;
//storedDataArray=resultantData;
//console.log(storedDataArray)
// storedDataArray.forEach(element => {
// gotData[index]=JSON.parse(element);
// console.log(gotData[index])
// index++;
// })
// console.log(gotData.length);
res.status(200).json(resultantData);
});
Is there any way to show temperature and other quantities in react table?
React-Table
class deviceData extends Component {
constructor(props) {
super(props)
this.state = {
dataList:[],
data : ' '
};
}
componentDidMount(){
const url="http://localhost:5000/api/data/viewData";
fetch (url,{
method: "GET"
}).then(response=> response.json()).then(result=>{
console.log(result);
this.setState({
dataList : result,
});
});
}
render() {
const columns =[
{
Header:"Message ID",
accessor:"_id",
sortable: true,
filterable: false,
style:{
textAlign: "left"
},
width: 300,
maxWidth: 100,
minWidth: 100,
},
{
Header:"Time Stamp",
accessor:"timestamp",
width: 300,
maxWidth: 100,
minWidth: 100,
},
{
Header:"Temperature",
id:'temperature',
filterable: false,
accessor: 'temperature'
},
{
Header:"Pressure",
id:'pressure',
filterable: false,
accessor: 'pressure'
},
{
Header:"Humidity",
id:'humidity',
filterable: false,
accessor: 'humidity'
},
]
return(
<div className="ReactTable">
<ReactTable
columns={columns}
data={this.state.dataList}
defaultPageSize={10}
className="-striped -highlight"
>
</ReactTable>
<div id={"#"+ this.props.id} ></div>
</div>
);
}
}
[![React-Table][1]][1]
Backend Response
[
{
"_id": "5dd3be2fcf55e1ec388f502c",
"timestamp": 1574157871067,
"message": "{\"temperature\":93,\"humidity\":94,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2ecf55e1ec388f502b",
"timestamp": 1574157870567,
"message": "{\"temperature\":58,\"humidity\":59,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2ecf55e1ec388f502a",
"timestamp": 1574157870067,
"message": "{\"temperature\":78,\"humidity\":79,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2dcf55e1ec388f5029",
"timestamp": 1574157869567,
"message": "{\"temperature\":88,\"humidity\":89,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2dcf55e1ec388f5028",
"timestamp": 1574157869066,
"message": "{\"temperature\":99,\"humidity\":100,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2ccf55e1ec388f5027",
"timestamp": 1574157868567,
"message": "{\"temperature\":38,\"humidity\":39,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2ccf55e1ec388f5026",
"timestamp": 1574157868067,
"message": "{\"temperature\":82,\"humidity\":83,\"pressure\":\"1 bar\"}"
},
{
"_id": "5dd3be2bcf55e1ec388f5025",
"timestamp": 1574157867566,
"message": "{\"temperature\":76,\"humidity\":77,\"pressure\":\"1 bar\"}"
}
]
Convert string back to object by using parse()
ex: var object = JSON.parse(str);
Important thing is to define column with correct accessor. Try this one:
const columns = [
{
Header: "Id",
accessor: "_id"
},
{
Header: "timestamp",
accessor: "timestamp"
},
{
Header: "Temprature",
accessor: "message.temprature"
},
{
Header: "humidity",
accessor: "message.humidity"
},
{
Header: "pressure",
accessor: "message.pressure"
}
];
And Use it in React-table like this:
<ReactTable
data={loans} // Instead of loans, use variable where you store your response
columns={columns}
defaultPageSize={10}
sortable={true}
/>
I think you can try with adding a headers in your fetch method
fetch (url,{
method: "GET",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
}
})
It will ensure your response is JSON
N.B. Try your url with postman first, setting those headers and see whether result is JSON or not, if it is JSON I believe my code will help you, if it is not a JSON return try to change your back-end code to ensure it return JSON using postman

how to display the api data using jsreport studio

Html Code:
<h3>Hello Welcome</h3>
<div>{{jsonData}}</div>
Script Code:
function beforeRender(req, res, done) {
require('request')({
url:'http://samples.openweathermap.org/data/2.5/weather? lat=35&lon=139&appid=b1b15e88fa797225412429c1c50c122a1',
json:true,
method: 'GET'
}, function(err, response, body){
console.log(JSON.stringify(body));
req.data = { jsonData: body };
done();
});
}
The api is returns following Json Data:
{
"coord": {
"lon": 139.01,
"lat": 35.02
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 285.514,
"pressure": 1013.75,
"humidity": 100,
"temp_min": 285.514,
"temp_max": 285.514,
"sea_level": 1023.22,
"grnd_level": 1013.75
},
"wind": {
"speed": 5.52,
"deg": 311
},
"clouds": {
"all": 0
},
"dt": 1485792967,
"sys": {
"message": 0.0025,
"country": "JP",
"sunrise": 1485726240,
"sunset": 1485763863
},
"id": 1907296,
"name": "Tawarano",
"cod": 200
}
This is the jsreport generation code. Now am trying to get the data through the rest api. I don't know how the api data is print in console, i need help to how to iterate using jsrender following json data and display in console.
the out will comes in all the jsreport http ajax in following object in script section
"jsreport.data"
In this object will have all our data. you just print the following code in script
console.log("data is available are :",jsreport.data);

Where do I find the Instagram media ID of a image

I'm am looking for the MediaID of an Instagram image which has been uploaded. It should look like
1234567894561231236_33215652
I have found out the last set of integers are the usersID
For example: this is the link for the image directly, however I see no mediaID in the correct format?
http://distilleryimage11.ak.instagram.com/d33aafc8b55d11e2a66b22000a9f09de_7.jpg
while this is the link
http://instagram.com/p/Y7GF-5vftL/
I don't wish to use the API as all I need the MediaID from a selected image.
Here's a better way:
http://api.instagram.com/oembed?url=http://instagram.com/p/Y7GF-5vftL/
Render as json object and you can easily extract media id from it ---
For instance, in PHP
$api = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/Y7‌​GF-5vftL/");
$apiObj = json_decode($api,true);
$media_id = $apiObj['media_id'];
For instance, in JS
$.ajax({
type: 'GET',
url: 'http://api.instagram.com/oembed?callback=&url=http://instagram.com/p/Y7GF-5vftL‌​/',
cache: false,
dataType: 'jsonp',
success: function(data) {
try{
var media_id = data[0].media_id;
}catch(err){}
}
});
So the most up-voted "Better Way" is a little deprecated so here is my edit and other solutions:
Javascript + jQuery
$.ajax({
type: 'GET',
url: 'http://api.instagram.com/oembed?callback=&url='+Url, //You must define 'Url' for yourself
cache: false,
dataType: 'json',
jsonp: false,
success: function (data) {
var MediaID = data.media_id;
}
});
PHP
$your_url = "" //Input your url
$api = file_get_contents("http://api.instagram.com/oembed?callback=&url=" . your_url);
$media_id = json_decode($api,true)['media_id'];
So, this is just an updated version of #George's code and is currently working. However, I made other solutions, and some even avoid an ajax request:
Shortcode Ajax Solution
Certain Instagram urls use a shortened url syntax. This allows the client to just use the shortcode in place of the media id if requested properly.
An example shortcode url looks like this: https://www.instagram.com/p/Y7GF-5vftL/
The Y7GF-5vftL is your shortcode for the picture.
Using Regexp:
var url = "https://www.instagram.com/p/Y7GF-5vftL/"; //Define this yourself
var Key = /p\/(.*?)\/$/.exec(url)[1];
In the same scope, Key will contain your shortcode. Now to request, let's say, a low res picture using this shortcode, you would do something like the following:
$.ajax({
type: "GET",
dataType: "json",
url: "https://api.instagram.com/v1/media/shortcode/" + Key + "?access_token=" + access_token, //Define your 'access_token'
success: function (RawData) {
var LowResURL = RawData.data.images.low_resolution.url;
}
});
There is lots of other useful information, including the media id, in the returned RawData structure. Log it or look up the api documentation to see.
Shortcode Conversion Solution
You can actually convert your shortcode to the id fairly easily! Here's a simple way to do it in javascript:
function shortcodeToInstaID(Shortcode) {
var char;
var id = 0;
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
for (var i = 0; i < Shortcode.length; i++) {
char = Shortcode[i];
id = (id * 64) + alphabet.indexOf(char);
}
return id;
}
Note: If you want a more robust node.js solution, or want to see how you would convert it back, check out #Slang's module on npm.
Full Page Solution
So what if you have the URL to a full Instagram page like: https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/
Well, you can actually read the HTML to find a meta property that contains the Media ID. There are also a couple other algorithms you can perform on the URL itself to get it, but I believe that requires too much effort so we will keep it simple. Either query the meta tag al:ios:url or iterate through the html. Since reading metatags is posted all over, I'll show you how to iterate.
NOTE: This is a little unstable and is vulnerable to being patched. This method does NOT work on a page that uses a preview box. So if you give it the current HTML when you click on a picture in someone's profile, this WILL break and return a bad Media ID.
function getMediaId(HTML_String) {
var MediaID = "";
var e = HTML_String.indexOf("al:ios:url") + 42; //HTML_String is a variable that contains all of the HTML text as a string for the current document. There are many different ways to retrieve this so look one up.
for (var i = e; i <= e + 100; i++) { //100 should never come close to being reached
if (request.source.charAt(i) == "\"")
break;
MediaID += request.source.charAt(i);
}
return MediaID;
}
And there you go, a bunch of different ways to use Instagram's api to get a Media ID. Hope one fixes your struggles.
Here's an even better way:
No API calls! And I threw in converting a media_id to a shortcode as an added bonus.
Based on slang's amazing work for figuring out the conversion. Nathan's work converting base10 to base64 in php. And rgbflawed's work converting it back the other way (with a modified alphabet). #teameffort
function mediaid_to_shortcode($mediaid){
if(strpos($mediaid, '_') !== false){
$pieces = explode('_', $mediaid);
$mediaid = $pieces[0];
$userid = $pieces[1];
}
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$shortcode = '';
while($mediaid > 0){
$remainder = $mediaid % 64;
$mediaid = ($mediaid-$remainder) / 64;
$shortcode = $alphabet{$remainder} . $shortcode;
};
return $shortcode;
}
function shortcode_to_mediaid($shortcode){
$alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$mediaid = 0;
foreach(str_split($shortcode) as $letter) {
$mediaid = ($mediaid*64) + strpos($alphabet, $letter);
}
return $mediaid;
}
Here's a much better way (No Instagram API):
Get the permalink URL of the Instagram image you need the media ID for. http://instagram.com/p/Y7GF-5vftL/ 👀
add ?__a=1 at the end of the permalinkhttp://instagram.com/p/Y7GF-5vftL/?__a=1 👀
Run the link in a browser and Instagram will provide all the metadata of the image in JSON, from which you can easily recover the image ID and other valuable data.
The JSON response will look like this:
{
"graphql": {
"shortcode_media": {
"__typename": "GraphImage",
"id": "448979387270691659",
"shortcode": "Y7GF-5vftL",
"dimensions": {
"height": 612,
"width": 612
},
"gating_info": null,
"fact_check_overall_rating": null,
"fact_check_information": null,
"sensitivity_friction_info": null,
"sharing_friction_info": {
"should_have_sharing_friction": false,
"bloks_app_url": null
},
"media_overlay_info": null,
"media_preview": null,
"display_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
"display_resources": [
{
"src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
"config_width": 640,
"config_height": 640
},
{
"src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
"config_width": 750,
"config_height": 750
},
{
"src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
"config_width": 1080,
"config_height": 1080
}
],
"accessibility_caption": "Photo by Steven Caelius Thirlwall on May 05, 2013.",
"is_video": false,
"tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjpmYWxzZSwidXVpZCI6IjFjOTliZDQ5MTU0YzQ2ODY4OGY5MmM4ODgwYWQ4NGQzNDQ4OTc5Mzg3MjcwNjkxNjU5Iiwic2VydmVyX3Rva2VuIjoiMTYzNzY4NDc1NjI4MHw0NDg5NzkzODcyNzA2OTE2NTl8MzExMTA2NDAyfDE2ZGMyNjk3M2M1YWY5YWEzOTNhZTY5YzEzYzU4YjM5NWI0YWQ3MjY1OGQxOTg4YWQ2OWUxYmI3ZjkyNzU5ZDQifSwic2lnbmF0dXJlIjoiIn0=",
"upcoming_event": null,
"edge_media_to_tagged_user": {
"edges": []
},
"edge_media_to_caption": {
"edges": [
{
"node": {
"text": "New ankle biter! #Beagle #pup #shoes #blueisthecolor #cute #iwantone"
}
}
]
},
"can_see_insights_as_brand": false,
"caption_is_edited": false,
"has_ranked_comments": false,
"like_and_view_counts_disabled": false,
"edge_media_to_parent_comment": {
"count": 902,
"page_info": {
"has_next_page": true,
"end_cursor": "QVFBZFlTbmNsVWh1T2dIS1I3Mm8yRl9DdGVQMHV6VXg5cjQxZzlYa0gxQ1NFZGc5a1FfWHhIMTlURU84dlBCcG5QX1I2VXhvVXNpbGRGWlktNG5FTjdRTQ=="
},
"edges": [
{
"node": {
"id": "17916614443887715",
"text": "#h___ep10",
"created_at": 1632633242,
"did_report_as_spam": false,
"owner": {
"id": "7472159900",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/33736445_386998071786552_2814599361646821376_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=100&_nc_ohc=kaD1UHiVnU8AX9oWv0I&edm=AABBvjUBAAAA&ccb=7-4&oh=cabcfaac8d48b63b41f0e9a32bb0282f&oe=61A38CDE&_nc_sid=83d603",
"username": "x._raha.moradi_.x"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17894221358363984",
"text": "#zahraroshanikia",
"created_at": 1632633244,
"did_report_as_spam": false,
"owner": {
"id": "46440556914",
"is_verified": false,
"profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/159256912_431734447917250_5641996282890612011_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=110&_nc_ohc=y2JgjQ2mc6EAX8U9OnZ&edm=AABBvjUBAAAA&ccb=7-4&oh=95911a34aea9f1fe0f6ae941154de86b&oe=61A4F7B5&_nc_sid=83d603",
"username": "saman.wx68"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17931125308699029",
"text": "#azarimani55",
"created_at": 1632633244,
"did_report_as_spam": false,
"owner": {
"id": "33210479560",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/94635563_230560861379228_5419754827787796480_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=7JyeFrDCzcMAX_uDrhk&edm=AABBvjUBAAAA&ccb=7-4&oh=5599a78e5508ca3827b5c580c4e5daf0&oe=61A3CB8E&_nc_sid=83d603",
"username": "alirezaekhteraee"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "18164016367163647",
"text": "#mbahrambagi436",
"created_at": 1632633245,
"did_report_as_spam": false,
"owner": {
"id": "45915987079",
"is_verified": false,
"profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/148716764_428606351708713_8819828798031017255_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=ZKBGqc8f6xAAX_roxPf&edm=AABBvjUBAAAA&ccb=7-4&oh=0b8ed79236bf5e97dde3a4621a1cc647&oe=61A3B2BD&_nc_sid=83d603",
"username": "xx77llxxrdd"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17921916424799580",
"text": "#soheila.rezvan",
"created_at": 1632633246,
"did_report_as_spam": false,
"owner": {
"id": "47289012029",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
"username": "ha_midreza2843"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17907564257125873",
"text": "#paria_niazi80",
"created_at": 1632633247,
"did_report_as_spam": false,
"owner": {
"id": "46492412778",
"is_verified": false,
"profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/260070060_924734438469169_5591668570031114480_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=fl-bN9uNz9sAX_v_XtR&edm=AABBvjUBAAAA&ccb=7-4&oh=a875da2937ac853d3111499bc32fa452&oe=61A43C46&_nc_sid=83d603",
"username": "ricky_gutierrezz4"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17922561976794017",
"text": "#memmm.mom",
"created_at": 1632633247,
"did_report_as_spam": false,
"owner": {
"id": "47086299475",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
"username": "izadiizadi2"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "18130172461216294",
"text": "#221bcc",
"created_at": 1632633247,
"did_report_as_spam": false,
"owner": {
"id": "47371753534",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
"username": "karimov_94422"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17932389193640009",
"text": "#tehran___la",
"created_at": 1632633248,
"did_report_as_spam": false,
"owner": {
"id": "46622156073",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
"username": "rynwrynw627"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17935588228640366",
"text": "#sepideh.m.i.r",
"created_at": 1632633248,
"did_report_as_spam": false,
"owner": {
"id": "5905800882",
"is_verified": false,
"profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/160110991_451499282792610_787693123774809640_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=CBgOU0M1MPYAX8m7vvT&edm=AABBvjUBAAAA&ccb=7-4&oh=18a215ed2c3a0cd0e58a905e22cd0c8f&oe=61A489B4&_nc_sid=83d603",
"username": "__mohammad__677"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17854564814634983",
"text": "#nilooofar5828",
"created_at": 1632633249,
"did_report_as_spam": false,
"owner": {
"id": "46091925856",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
"username": "hsheh6154"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
},
{
"node": {
"id": "17907793715125698",
"text": "#oran31201816",
"created_at": 1632633249,
"did_report_as_spam": false,
"owner": {
"id": "44672434922",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/149881733_183016319827876_8572211010018355650_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=104&_nc_ohc=JgjvztBQGrIAX8nLUiM&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=d9e408cf78f195a83f8bb7beec8c22c1&oe=61A360EC&_nc_sid=83d603",
"username": "barcelona10_trol"
},
"viewer_has_liked": false,
"edge_liked_by": {
"count": 0
},
"is_restricted_pending": false,
"edge_threaded_comments": {
"count": 0,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": []
}
}
}
]
},
"edge_media_to_hoisted_comment": {
"edges": []
},
"edge_media_preview_comment": {
"count": 902,
"edges": []
},
"comments_disabled": false,
"commenting_disabled_for_viewer": false,
"taken_at_timestamp": 1367742535,
"edge_media_preview_like": {
"count": 13823,
"edges": [
{
"node": {
"id": "750983393",
"is_verified": false,
"profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/245113529_402184758053910_6991076024439577169_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=TPs3jHagdx4AX_JdrO5&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=95f6532e2d737a91e3848fa0525319e4&oe=61A4C0D1&_nc_sid=83d603",
"username": "josepkoray"
}
}
]
},
"edge_media_to_sponsor_user": {
"edges": []
},
"is_affiliate": false,
"is_paid_partnership": false,
"location": null,
"viewer_has_liked": false,
"viewer_has_saved": false,
"viewer_has_saved_to_collection": false,
"viewer_in_photo_of_you": false,
"viewer_can_reshare": true,
"owner": {
"id": "45818965",
"is_verified": false,
"profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/38792937_2121773964703196_2247098649857228800_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=LyE1N2PfQ3IAX9R25jq&edm=AABBvjUBAAAA&ccb=7-4&oh=29912db673500a2adc9df6ec7241d8c7&oe=61A471D2&_nc_sid=83d603",
"username": "taz4535",
"blocked_by_viewer": false,
"restricted_by_viewer": false,
"followed_by_viewer": false,
"full_name": "Steven Caelius Thirlwall",
"has_blocked_viewer": false,
"is_embeds_disabled": false,
"is_private": false,
"is_unpublished": false,
"requested_by_viewer": false,
"pass_tiering_recommendation": false,
"edge_owner_to_timeline_media": {
"count": 236
},
"edge_followed_by": {
"count": 700
}
},
"is_ad": false,
"edge_web_media_to_related_media": {
"edges": []
},
"coauthor_producers": [],
"edge_related_profiles": {
"edges": []
}
}
}
}
You can use the same ?__a=1 ending even on Instagram user permalinks, for example https://www.instagram.com/taz4535/?__a=1 👀 and you'll get incredibly valuable user data in the reply JSON.
The best is that this is all done without Instagram API authentication!
You can actually derive the MediaId from the last segment of the link algorithmically using a method I wrote about here: http://carrot.is/coding/instagram-ids. It works by mapping the URL segment by character codes & converting the id into a base 64 number.
For example, given the link you mentioned (http://instagram.com/p/Y7GF-5vftL), we get the last segment (Y7GF-5vftL) then we map it into character codes using the base64 url-safe alphabet (24:59:6:5:62:57:47:31:45:11_64). Next, we convert this base64 number into base10 (448979387270691659).
If you append your userId after an _ you get the full id in the form you specified, but since the MediaId is unique without the userId you can actually omit the userId from most requests.
Finally, I made a Node.js module called instagram-id-to-url-segment to automate this conversion:
convert = require('instagram-id-to-url-segment');
instagramIdToUrlSegment = convert.instagramIdToUrlSegment;
urlSegmentToInstagramId = convert.urlSegmentToInstagramId;
instagramIdToUrlSegment('448979387270691659'); // Y7GF-5vftL
urlSegmentToInstagramId('Y7GF-5vftL'); // 448979387270691659
Try the solution from this question:
How can I get an direct Instagram link from a twitter entity?
You can get just the image by appending /media/ to the URL. Using your
You can even specify a size,
One of t (thumbnail), m (medium), l (large). Defaults to m.
So for a thumbnail: http://instagr.am/p/QC8hWKL_4K/media/?size=t
Here is python solution to do this without api call.
def media_id_to_code(media_id):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
short_code = ''
while media_id > 0:
remainder = media_id % 64
media_id = (media_id-remainder)/64
short_code = alphabet[remainder] + short_code
return short_code
def code_to_media_id(short_code):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
media_id = 0;
for letter in short_code:
media_id = (media_id*64) + alphabet.index(letter)
return media_id
Your media ID is: 448979387270691659_45818965
This is how to get it.
Go to instgram.com/username.
Click the photo you want the id of.
(Chrome instructions) right click on the photo (should be a popup image)
Inspect element
Search through the selected text, you should see something like this photo448979387270691659_45818965
There should be your photo ID.
For some reason, this only seems to work with the popup, and not the actual image URL.
In pure JS (provided your browser can handle XHRs, which every major browser [including IE > 6] can):
function igurlretrieve(url) {
var urldsrc = "http://api.instagram.com/oembed?url=" + url;
//fetch data from URL data source
var x = new XMLHttpRequest();
x.open('GET', urldsrc, true);
x.send();
//load resulting JSON data as JS object
var urldata = JSON.parse(x.responseText);
//reconstruct data as "instagram://" URL that can be opened in iOS app
var reconsturl = "instagram://media?id=" + urldata.media_id;
return reconsturl;
}
Provided this is your goal -- simply opening the page in the Instagram iOS app, which is exactly what this is about -- this should do, especially if you don't want to have to endure licensing fees.
edit
The iOS Instagram app has now registered for regular http links to open in the Instagram app and this deeplink methodology is no longer necessary.
old
Swift 4 short-code parsing solution
private static func instagramDeepLinkFromHTTPLink(_ link: String) -> String? {
guard let shortcode = link.components(separatedBy: "/").last else { return nil }
// algorithm from https://stackoverflow.com/a/37246231/337934
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
var mediaId: Int = 0
for (_, char) in shortcode.enumerated() {
guard let index = alphabet.index(of: char) else { continue }
mediaId = (mediaId * 64) + index.encodedOffset
}
return "instagram://media?id=\(mediaId)"
}
Same thing you can implement in Python-
import requests,json
def get_media_id(media_url):
url = 'https://api.instagram.com/oembed/?callback=&url=' + media_url
response = requests.get(url).json()
print(response['media_id'])
get_media_id('MEDIA_URL')
You can use the shortcode media API from instagram. If you use php you can use the following code to get the shortcode from the image's URL:
$matches = [];
preg_match('/instagram\.com\/p\/([^\/]*)/i', $url, $matches);
if (count($matches) > 1) {
$shortcode = $matches[1];
}
Then send a request to the API using your access token (Replace ACCESS-TOKEN with your token)
$apiUrl = sprintf("https://api.instagram.com/v1/media/shortcode/%s?access_token=ACCESS-TOKEN", $shortcode);
Instagram deprecated their legacy APIs in support for Basic Display API during the late 2019
In Basic Display API you are supposed to use the following API endpoint to get the media id. You will need to supply a valid access token.
https://graph.instagram.com/me/media?fields=id,caption&access_token={access-token}
You can read here on how to configure test account and generate access token on Facebook developer portal.
Here is another article which also describes about how to get access token.
Instagram media id to Shortcode
Instagram Shortcode to media id
var bigint = require( 'big-integer' )
var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var numbers = '0123456789'
var ig_alphabet = upper + lower + numbers + '-_'
var bigint_alphabet = numbers + lower
function toShortcode( longid )
{
var o = bigint( longid ).toString( 64 )
return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) =>
{
return ig_alphabet.charAt( ( m1 )
? parseInt( m1 )
: bigint_alphabet.indexOf( m2 ) )
});
}
function fromShortcode( shortcode )
{
var o = shortcode.replace( /\S/g, m =>
{
var c = ig_alphabet.indexOf( m )
var b = bigint_alphabet.charAt( c )
return ( b != "" ) ? b : `<${c}>`
} )
return bigint( o, 64 ).toString( 10 )
}
toShortcode( '908540701891980503' ) // s.b. 'ybyPRoQWzX'
fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503'
Right click on a photo and open in a new tab/window. Right click on inspect element. Search for:
instagram://media?id=
This will give you:
instagram://media?id=############# /// the ID
The full id construct from
photoID_userID
To get the user id, search for:
instapp:owner_user_id Will be in content=

Resources