首页 文章

如何获得Instagram访问令牌

提问于
浏览
67

我真的在努力获取Instagram的访问令牌,

我已经注册了一个新客户端,然后我使用了这个URL

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

填写客户ID并重定向Url .

然后我被重定向到一个页面,它在Url中显示了一个代码但是从那里我没有线索,其中id然后获取我的访问令牌 .

12 回答

  • 0

    如果您正在寻找说明,请查看本文post . 如果您使用的是C#ASP.NET,请查看此repo .

  • 0

    通过使用https://www.hurl.it/我能够看到:{"code":400,"error_type":"OAuthException","error_message":"Matching code was not found or was already used."}

    所以:你必须为每个请求获取新代码 .

  • 1

    这对我来说很好用:

    http://jelled.com/instagram/access-token

    仅供参考,我将它与jQuery Instagram插件结合使用,你可以在这里找到它; http://potomak.github.com/jquery-instagram

  • 1

    链接到官方API文档是http://instagram.com/developer/authentication/

    Longstory short - 两步:

    Get CODE

    打开https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code,其中包含来自http://instagram.com/developer/clients/manage/的信息

    Get access token

    curl \-F 'client_id=CLIENT-ID' \
        -F 'client_secret=CLIENT-SECRET' \
        -F 'grant_type=authorization_code' \
        -F 'redirect_uri=YOUR-REDIRECT-URI' \
        -F 'code=CODE' \
        https://api.instagram.com/oauth/access_token
    
  • 39
  • 0

    到目前为止,人们发布的几乎所有回复都只涉及如何在Instagram的客户端“隐式认证”程序之后处理前端的访问令牌 . 根据Instagram的API文档,这种方法不太安全,也不推荐 .

    假设您正在使用服务器,Instagram文档在提供有关交换令牌代码的明确答案时失败,因为它们仅提供了cURL请求的示例 . 基本上,您必须使用提供的代码和所有应用程序的信息向其服务器发出POST请求,并且它们将返回包含用户信息和令牌的用户对象 .

    我不知道你在写什么语言,但是我在Node.js中用请求npm模块解决了这个问题,你可以找到here .

    我通过网址解析并使用此信息发送帖子请求

    var code = req.url.split('code=')[1];
    
    request.post(
      { form: { client_id: configAuth.instagramAuth.clientID,
                client_secret: configAuth.instagramAuth.clientSecret,
                grant_type: 'authorization_code',
                redirect_uri: configAuth.instagramAuth.callbackURL,
                code: code
              },
        url: 'https://api.instagram.com/oauth/access_token'
      },
      function (err, response, body) {
        if (err) {
          console.log("error in Post", err)
        }else{
          console.log(JSON.parse(body))
        }
      }
    );
    

    当然用您自己的信息替换configAuth . 您可能没有使用Node.js,但希望此解决方案可以帮助您将自己的解决方案转换为您使用它的任何语言 .

  • 2

    Instagram API不仅适用于您,也适用于任何可能通过您的应用进行身份验证的Instagram用户 . 我跟着instructions on the Instagram Dev website . 使用第一个(显式)方法,我能够在服务器上轻松地完成此操作 .

    Step 1) 在您的网页上添加一个链接或按钮,用户可以单击该按钮或按钮以启动身份验证过程:

    <a href="https://api.instagram.com/oauth/authorize/?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code">Get Started</a>
    

    在Instagram后端成功注册您的应用程序后,将向您提供 YOUR_CLIENT_IDYOUR_REDIRECT_URI ,以及下面使用的 YOUR_CLIENT_SECRET .

    Step 2) 在您为应用定义的URI(与 YOUR_REDIRECT_URI 相同)处,您需要接受来自Instagram服务器的响应 . Instagram服务器将在请求中返回 code 变量 . 然后,您需要使用此 code 以及有关您的应用程序的其他信息,直接从您的服务器发出另一个请求以获取 access_token . 我在使用Django框架的python中做到了这一点,如下所示:

    将django直接发送到 urls.py 中的 response 函数:

    from django.conf.urls import url
    
    from . import views
    
    app_name = 'main'
    urlpatterns = [
            url(r'^$', views.index, name='index'),
            url(r'^response/', views.response, name='response'),
    ]
    

    这是 response 函数,处理请求, views.py

    from django.shortcuts import render
    import urllib
    import urllib2
    import json
    
    def response(request):
        if 'code' in request.GET:
            url = 'https://api.instagram.com/oauth/access_token'
            values = {
                'client_id':'YOUR_CLIENT_ID',
                'client_secret':'YOUR_CLIENT_SECRET',
                'redirect_uri':'YOUR_REDIRECT_URI',
                'code':request.GET.get('code'),
                'grant_type':'authorization_code'
            }
            data = urllib.urlencode(values)
            req = urllib2.Request(url, data)
            response = urllib2.urlopen(req)
            response_string = response.read()
            insta_data = json.loads(response_string)
            if 'access_token' in insta_data and 'user' in insta_data:
                #authentication success
                return render(request, 'main/response.html')
            else:
                #authentication failure after step 2
                return render(request, 'main/auth_error.html')
        elif 'error' in req.GET:
            #authentication failure after step 1
            return render(request, 'main/auth_error.html')
    

    这只是一种方式,但在PHP或任何其他服务器端语言中,该过程应该几乎相同 .

  • 9

    在安全认证下禁用隐式oauth,然后加载:

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

    在您的帐户中指定REDIRECT-URI并完全按照指定键入 .

  • 0

    授权应用程序使用Instagram数据后,访问令牌将作为URI片段返回 . 它应该类似于以下内容:
    enter image description here

  • 9

    试试这个:

    http://dmolsen.com/2013/04/05/generating-access-tokens-for-instagram/

    获取代码后,您可以执行以下操作:

    curl -F 'client_id=[your_client_id]' -F 'client_secret=[your_secret_key]' -F 'grant_type=authorization_code' -F 'redirect_uri=[redirect_url]' -F 'code=[code]' https://api.instagram.com/oauth/access_token
    
  • -2

    如果您不想构建服务器端,例如仅在客户端(Web应用程序或移动应用程序)上进行开发,则可以选择 Implicit Authentication .

    正如文档所述,首先使用https请求

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

    填写您指定的 CLIENT-IDREDIRECT-URL .

    然后,这将进入登录页面,但最重要的是如何在用户正确登录后获取访问令牌 .

    在用户使用正确的帐户和密码单击登录按钮后,该网页将重定向到您指定的URL,然后是新的访问令牌 .

    http:// your-redirect-uri#access_token = ACCESS-TOKEN

    我'm not familiar with javascript , but in Android studio , that'是一个简单的方法 add a listener which listen to the event the web page override the url to the new url (重定向事件),然后它会将重定向url字符串传递给你,这样你就可以轻松拆分它以获得访问令牌,如:

    String access_token = url.split(“=”)[1];

    意味着将url分解为每个“=”字符中的字符串数组,然后访问令牌显然存在于[1] .

  • 5

    去管理clinet页面:

    http://www.instagram.com/developer/设置重定向网址

    然后 :

    使用此代码获取访问令牌:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>tst</title>
    <script src="../jq.js"></script>
    <script type="text/javascript">
           $.ajax({
                type: 'GET',
                url: 'https://api.instagram.com/oauth/authorize/?client_id=CLIENT-‌​ID&redirect_uri=REDI‌RECT-URI&response_ty‌pe=code'
                dataType: 'jsonp'}).done(function(response){
                    var access = window.location.hash.substring(14);
                    //you have access token in access var   
                });
    </script>
    </head>
    <body>  
    </body>
    </html>
    

相关问题