2016-06-17 61 views
0

我正在玩instagram api和请求,试图通过OAuth。我使用r = requests.get(...)并遵循instagram文档。然而,请求的输出并不是我所期望的。我有两个有点相似的问题。Python请求和instagram

1)如果我发送了错误的参数我希望要回JSON看起来像

{"code": 400, "error_type": "OAuthException", "error_message": "You must include a valid client_id, response_type, and redirect_uri parameters"} 

这是我所看到的,如果我粘贴字符串从r.url到浏览器中。然而r.json()只是失败,给我

JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

2)正确的反应应该我重定向到我的重定向URL,这也将包括一个代码。再次,当将r.url粘贴到浏览器中时,此功能会正常工作。我按预期成功重定向。请求似乎不会重定向我,即使明确设置了allow_redirects=False。我只需要从网址中读取代码。我怎样才能得到请求给我最终的URL来提取代码?

编辑:不妨包括一些代码。

def get_code(client_id, redirect_uri, scope="basic"): 
    params = {"client_id": client_id, 
       "redirect_uri": redirect_uri, 
       "response_type": "code", 
       "scope": scope} 
    r = requests.get('https://api.instagram.com/oauth/authorize/', 
        params=params) 
    # print r.url 
    # print r.json() 
    return r 
+0

您如何验证?也是一个链接到文档将是好 –

+0

我曾经尝试隐式和显式方法(它们本质上是相同的这一步)。这里是文档:https://www.instagram.com/developer/authentication/ – Shatnerz

+0

json不会被解析,因为它是一个登录表单的html。尝试在浏览器中清除您的Cookie,然后您应该看到登录表单。 –

回答

1

它告诉你在documentation究竟发生什么,在这一点上,我们提出了一个登录屏幕的用户,然后确认屏幕,在这里给予你的应用程序能接触到她的Instagram数据,如果您运行下面的代码段会在您的浏览器中看到一个登录框:

import requests 
import webbrowser 
from tempfile import NamedTemporaryFile 
params = {"client_id": client_id, 
      "redirect_uri": redirect_uri, 
      "response_type": "code", 
      "scope": scope} 
with NamedTemporaryFile(dir=".", delete=0) as f: 
    r = requests.get('https://api.instagram.com/oauth/authorize/', 
        params=params) 
    f.write(r.content) 
    webbrowser.open(f.name)