javascript Instagram如何获取access_token

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10552091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:13:42  来源:igfitidea点击:

Instagram how to get access_token

javascriptinstagram

提问by Piotr Podgorski

I'm new to Instagram apps development and struggling with this issue for some time now. Basically, this is how it looks like:

我是 Instagram 应用程序开发的新手,并且在这个问题上挣扎了一段时间。基本上,它是这样的:

I'm redirecting user to authorization url like this:

我将用户重定向到授权 url,如下所示:

window.open("https://api.instagram.com/oauth/authorize/?client_id=" + igClientId + "&redirect_uri=" + igCallbackUrl + "&response_type=code", "_blank");

Then, when user will login, page reloads and Instagram CODE is added at the end of callback url ('?code=returned_by_instagram_code').

然后,当用户登录时,页面会重新加载,并在回调 url (' ?code=returned_by_instagram_code')的末尾添加 Instagram 代码。

And this is the place where I've stuck.

这就是我卡住的地方。

How can I obtain returned code from url after page reloads (it is stored in cookies or session so I could access it in some way, or should I get it somehow through callback url and some function attached to it)?

如何在页面重新加载后从 url 获取返回的代码(它存储在 cookie 或 session 中,以便我可以以某种方式访问​​它,或者我应该通过回调 url 和附加的一些函数以某种方式获取它)?

How next, when I'll obtain the code, can I send (POST) request to Instagram for access_token?

接下来,当我获得代码时,我可以POST向 Instagram发送 ( ) 请求access_token吗?



Thank you in advance for any help.

预先感谢您的任何帮助。

PS. I'm using javascript without any frameworks (e.g. jquery) and this is the response that I'm looking for.

附注。我正在使用没有任何框架(例如 jquery)的 javascript,这是我正在寻找的响应。

回答by Phil Rasmussen

From the documentation on using Client-Side access token generation only (http://instagram.com/developer/authentication/) and the key thing you need to change is the responsecode in your URL to be: &response_type=token

从仅使用客户端访问令牌生成的文档(http://instagram.com/developer/authentication/)和您需要更改的关键是您的 URL 中的响应代码为: &response_type= token

Client-Side (Implicit) Authentication If you're building an app that does not have a server component (a purely javascript app, for instance), you'll notice that it's impossible to complete step three above to receive your access_token without also having to ship your client secret. You should never ship your client secret onto devices you don't control. Then how do you get an access_token? Well the smart folks in charge of the OAuth 2.0 spec anticipated this problem and created the Implicit Authentication Flow.

客户端(隐式)身份验证 如果您正在构建一个没有服务器组件的应用程序(例如,一个纯粹的 javascript 应用程序),您会注意到不可能完成上面的第三步来接收您的 access_token 而没有发送您的客户机密。您永远不应该将您的客户端机密发送到您无法控制的设备上。那么如何获得access_token呢?负责 OAuth 2.0 规范的聪明人预料到了这个问题并创建了隐式身份验证流程。

Step One: Direct your user to our authorization URL

第一步:将您的用户定向到我们的授权 URL

https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECTURI&response_type=token

At this point, we present the user with a login screen and then a confirmation screen where they approve your app's access to their Instagram data. Note that unlike the explicit flow the response type here is “token”.

此时,我们会向用户显示一个登录屏幕,然后是一个确认屏幕,他们会在其中批准您的应用访问其 Instagram 数据。请注意,与显式流不同,这里的响应类型是“令牌”。

Step Two: Receive the access_token via the URL fragment

第二步:通过URL片段接收access_token

Once the user has authenticated and then authorized your application, we'll redirect them to your redirect_uri with the access_token in the url fragment. It'll look like so:

一旦用户通过身份验证并授权您的应用程序,我们将使用 url 片段中的 access_token 将他们重定向到您的 redirect_uri。它看起来像这样:

http://your-redirect-uri#access_token=270740070.f59def8.d58713f2c81741c5b3686109306f98b4

Simply grab the access_token off the URL fragment and you're good to go. If the user chooses not to authorize your application, you'll receive the same error response as in the explicit flow

只需从 URL 片段中获取 access_token 即可。如果用户选择不授权您的应用程序,您将收到与显式流程中相同的错误响应

回答by Elton da Costa

A simple example, get your token in registration app on api.instagran (https://instagram.com/developer/clients/register/). Make your login before:

一个简单的例子,在 api.instagram ( https://instagram.com/developer/clients/register/)上的注册应用程序中获取您的令牌。请先登录:

var accessToken = '5e6e329062f047dd95ggj6c9df202c828';

$.ajax({
 url: 'https://api.instagram.com/v1/media/popular',
 dataType: 'jsonp',
 type: 'GET',
 data: {client_id: accessToken},
 success: function(data){
    console.log(data);
    for(x in data.data){
      $('ul').append('<li><img  src="'+data.data[x].images.low_resolution.url+'"></li>');  
    }
},
error: function(data){
    console.log(data);
}

});

});

回答by Subir Kumar Sao

The igCallbackUrl will redirect to a page. The code can be accessed as a GET parameter to that request.

igCallbackUrl 将重定向到一个页面。该代码可以作为该请求的 GET 参数访问。

Something like GET['code'] in php. If you give more details about the Server side framework then it will be helpful to answer your qwery.

类似 GET['code'] 在 php 中的东西。如果您提供有关服务器端框架的更多详细信息,那么回答您的问题会很有帮助。

You can use github.com/Instagram/instagram-javascript-sdk if you are using on Javascript.

如果您在 Javascript 上使用,您可以使用 github.com/Instagram/instagram-javascript-sdk。