小编mma*_*cio的帖子

Python请求访问OAUTH网站内容 - SNL财经

我一直在用头撞墙,试图从新闻来源“SNL 财经”​​检索内容。我拥有有效的凭据,因此理论上我应该能够以编程方式访问他们的新闻内容。

简而言之,我尝试执行以下脚本但没有成功:

s = requests.Session()

client_id = "..."
client_secret = "..."
token_url = "https://www.snl.com/SNL.Services.Security.Service/oauth/token"
protected_url = "https://www.snl.com/web/client?auth=inherit#news/article?id=40666532&KeyProductLinkType=14"

request_data = {
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://www.snl.com",
    "grant_type": "refresh_token",
    "refresh_token": refresh_token
}

token_response = s.post(token_url, data=request_data)
### token response is in jwt format, including token_type, expires_in, scope, etc. ###
token = json.loads(token_response.text)["access_token"].split('>')[1].split('<')[0]
request_data["token"] = token

article = s.post(protected_url, headers=request_data)
Run Code Online (Sandbox Code Playgroud)

可悲的是,这失败了。我最终得到了 200 响应,但它似乎只是登录页面(老实说,我不完全确定我在看什么)。

为了了解更多背景信息,我添加了在整个身份验证过程中填充的浏览器信息:

  1. 尝试访问受保护的url,重定向到以下url(省略snl基):

    /web/client?auth=inherit&contextType=external&username=string&enablePersistentLogin=true&OverrideRetryLimit=0&SwitchGetToPostLimit=50000&contextValue=%2Foam&password=secure_string&challenge_url=https%3A%2F%2Fwww.snl.com%2Fweb%2Fclient%3Fauth%3Dinherit&request_id=-6149669210818920852&authn_try_count=0&locale=en_US&resource_url=https%253A%252F%252Fwww.snl.com%252FInteractiveX%252FDefault.aspx%253Ftarget%253Dnews%25252Farticle%25253Fid%25253D40666532%252526KeyProductLinkType%25253D14%2526SNL3%253D1
    
    Run Code Online (Sandbox Code Playgroud)

此处显示请求标头。

  1. 输入登录名/密码后,将检索令牌并加载受保护的页面。

请求 cookie如下所示。

另外,我有点困惑为什么SNL_OAUTH_TOKEN上面链接(第二个链接)中的令牌值与我从脚本收到的 jwt 令牌响应中显示的值不同。

这里的任何指导将不胜感激。我也很乐意发送任何其他有用的非个人信息。

谢谢你!

python oauth python-requests

5
推荐指数
1
解决办法
6181
查看次数

标签 统计

oauth ×1

python ×1

python-requests ×1