【问题标题】:Requests for Instagram Access tokenInstagram 访问令牌的请求
【发布时间】:2017-08-24 13:38:36
【问题描述】:

我正在使用 Django(python) 开发 Instagram API

我正在从

获取代码
'https://api.instagram.com/oauth/authorize/?client_id=%s&response_type=code&redirect_uri=%s' % (INSTAGRAM_APP_CONSUMER_KEY, redirect_uri)

但是当我交换访问令牌代码的代码失败

# All arguments are valid 
def execute(self, code, redirect_uri, app_id, app_secret):
    exchange_url = 'https://api.instagram.com/oauth/access_token'
    try:
        #Update :  get request to post
        r = requests.post(exchange_url, params={
            'client_id': app_id,
            'redirect_uri': redirect_uri,
            'client_secret': app_secret,
            'code': code,
            'grant_type': 'authorization_code'

        })
        #print(r)
        #print(json.loads(r))
        print(r.json())
        return r.json()
    except Exception as e:
        print(e)

r.json() 给出 simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1

更新 1:r.json() 在请求从 get 更改为 post 但出错后工作 消息“您必须提供 client_id”

请告诉我我做错了什么

【问题讨论】:

  • 如果你只打印r会发生什么?
  • 这是一个错误Error 405。阅读此内容并找到另一种方式来请求您需要的任何内容。希望这会有所帮助
  • 还有可能您需要发出 POST 请求而不是 get 吗?
  • 在发布请求响应 400 和 r.json() 有效但 'error_message': 'You must provide a client_id' 而我提供 client_id

标签: json django python-requests instagram-api


【解决方案1】:

我认为它需要 post 数据中的数据,而不是查询参数中的数据。
试试这个:

your_post_data = {'client_id': '', ... }
r = requests.post('your_url', data=your_post_data)  

参考:Python Requests Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多