你做对了。截至 2016 年 6 月,任何 Instagram API 调用都需要访问令牌。
获取访问令牌的描述见文档 https://www.instagram.com/developer/authentication/。不需要应用程序批准。
There are two ways to get one: server-side or client-side. The second option (called implicit authentication) can only be used when implicit OAuth is enabled in the client settings (Manage Clients > Edit Client > Security > Disable implicit OAuth). It is disabled by default.
无论哪种情况,您都需要将用户重定向到授权 URL 以获取访问令牌。
显式模式(服务器端)的 URL 是:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code
隐式模式(客户端)的 URL 为:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token
之后您将被重定向到REDIRECT-URI
,将传递一个参数。对于显式模式,这将是一个带有code,而对于隐式模式,您将得到访问令牌直接作为哈希:
http://your-redirect-uri?code=CODE
http://your-redirect-uri#access_token=ACCESS-TOKEN
对于隐式模式,您可以从window.location.hash
在 JavaScript 中。
然而,对于显式模式,您需要进一步处理code获取访问令牌。您可以阅读如何做到这一点API文档 https://www.instagram.com/developer/authentication/。我不打算在这里进一步讨论这个问题。
问题是every想要查看您的动态的用户需要登录 Instagram(并拥有帐户)才能查看。就您而言,这可能是不需要的。然而,有一些选项可以解决这个(相当烦人的)问题:
You can reuse your own (already obtained) access token(s) to display the Instagram feed for every user. You will need to be aware of rate limits for each token. For sandboxed apps this is 500 API calls / hour, while live mode allows 5000 API calls / hour. [source https://www.instagram.com/developer/limits/] You could store tokens in a table and use them in a round-robin manner, to allow more API calls. This involves manually obtaining a bunch of tokens which your application can use (the more the better). This might not be the ideal solution considering Instagram doesn't warrant access tokens to have an unlimited lifetime.
您可以通过附加来检索 JSON 数据而无需身份验证/media/
到用户页面 URL,如中所述这个帖子 https://stackoverflow.com/a/33783840/192077。此操作不需要令牌或客户端 ID。但是,这仅适用于用户,不适用于标签。此外,Instagram 没有记录此功能,因此不保证将来可以使用。
您可以使用像这样的聚合器Juicer https://www.juicer.io/ or 对话提要 https://www.dialogfeed.com/相反,它将为您处理访问令牌。这通常不是免费的。
我还在为我的网站制作 Instagram feed,这是我从研究中得出的结论。请指出我犯的任何错误。
Edit:以下是沙箱应用程序的一些更多限制。
在沙盒模式下,您只能访问沙盒用户(即收到沙盒邀请的用户)的数据。这意味着:
- 用户检索的媒体,例如
/users/{user-id}/media/recent
,如果用户不是任何沙箱用户,将返回空响应。
- 通过标签检索媒体,例如
/tags/{tag-name}/media/recent
,将仅包含属于沙箱用户的标记媒体。
因此,要使标签源正常工作,它needs上线(经过审核和批准)。如果您不想这样做,唯一的选择是使用我上面提到的聚合器。