问题
In order to make request to the instagram api. I require an access token.
Which is documented here. http://instagram.com/developer/authentication/
These access token can change so i need to lookup via the api to get the "code" and then the access token. Most of the examples i see online prompt the user to instagram which then calls the callback url and off you go and generate the access_token.
How can i via the api ( and not logging into instragram ) get the inital "code"/
I was hoping for something like this.
var accessUrl = string.Format(
"https://instagram.com/oauth/authorize/?client_id={0}&redirect_uri={1}&response_type=code",
instagramClientId, redirectUrl);
WebRequest request = WebRequest.Create(accessUrl);
WebResponse response = request.GetResponse();
To be clear I'm not asking how to get the access_token but how do I get the "code" than generates the access_token without logging into instagram
回答1:
code
is appended as a url querystring in a GET
request that Instagram sends to your callback url.
For example, if your callback url is:
http://www.mycallbackurl.com/callback
Instagram (upon receiving your access_token request) will send a GET
call to:
http://mycallbackurl.com/callback?code=20938409823kjl2k3j4o998as0df809s80980234809
(it's base64 encoded).
You then need to capture that url parameter on your callback server and echo it out on the page (nothing else, only the value of the code
parameter), resulting in something like:
<html>
<body>20938409823kjl2k3j4o998as0df809s80980234809</body>
</html>
This can be done easily with php:
if (isset($_GET['code']))
echo $_GET['code'];
else
//do all the other stuff your page does
Hopefully if you're not using PHP you can discern how to accomplish the same result. This part of their authentication flow is rather poorly documented, but hopefully this helps.
Also: (Not sure if I understand this part of the question or not, but) the only authentication flow they allow is the in-browser login page. If you're trying to build your own custom login and pass credentials, it won't work.
来源:https://stackoverflow.com/questions/20526880/retrieving-instagram-access-token-via-api-in-c-sharp-a