使用python-instagram API订阅标签失败

1 投票
2 回答
673 浏览
提问于 2025-04-18 05:38

我正在尝试使用python-instagram这个库来订阅一个话题标签。之前我用cURL工具完成了身份验证,那个方法很好用。不过,我现在想用这个库提供的所有功能。

from instagram import client, subscriptions
from flask import Flask, request, render_template, session, redirect, abort, flash,     jsonify

app = Flask(__name__)

api = client.InstagramAPI(client_id="", client_secret="")

callback_url = 'http://localhost.com:515'
api.create_subscription(object='tag',object_id='bacon', aspect='media', callback_url=callback_url)

@app.route('/', methods=['GET'])
def handshake():
code = request.args.get('hub.challenge')
if code:
    return code 

if __name__ == '__main__':
    app.debug = True
app.run(host='localhost.com',port=515)

我遇到的错误是: 回溯(最近的调用最后一次):

  File "test.py", line 10, in <module>
    api.create_subscription(object='tag',object_id='bacon', aspect='media', callback_url=callback_url)
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 152, in _call
    return method.execute()
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 144, in execute
    content, next = self._do_api_request(url, method, body, headers)
  File "/usr/local/lib/python2.7/dist-packages/instagram/bind.py", line 100, in _do_api_request
    raise InstagramClientError('Unable to parse response, not valid JSON.')
instagram.bind.InstagramClientError: Unable to parse response, not valid JSON.

当我去掉create_subscription这一行时,程序似乎没问题,但create_subscription这个方法有点奇怪,我搞不清楚它到底在干嘛。我的最终目标是订阅这个标签,并接收带有这个标签的新图片。

2 个回答

0

文档写错了。你需要像这样创建一个InstagramAPI的实例:

client = InstagramAPI(access_token=my_access_token,
                      client_id=my_client_id,
                      client_secret=my_client_secret)

如果你需要进行身份验证,你得这样做:

client = InstagramAPI(client_id=my_client_id,
                      client_secret=my_client_secret,
                      redirect_uri="http://localhost")
authurl = client.get_authorize_login_url(scope=["basic", "relationships", "likes", "public_content"]) # or whatever scopes you need
0

这可能是由于各种问题造成的:

  1. 确保你的回调网址是可以访问的,其他地方的人能打开。

  2. 检查你的 handshake() 函数,确保它能返回正确的值(可以加一些打印语句来帮助调试)。

  3. 检查一下 /usr/local/lib/python2.7/dist-packages/instagram/bind.py 文件,看看里面的错误信息,这可能就是问题的所在。

  4. 让你的 handshake() 函数能够接受来自 Instagram 的 POST 请求。

  5. 在这种情况下,你可能需要使用经过认证的 API。

撰写回答