Skip to main content
 首页 » 编程设计

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

2025年02月15日6JustinYoung

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

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

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) 

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

有关更多背景信息,我在整个身份验证过程中包含了浏览器信息:

  1. 尝试访问 protected url,重定向到以下 url(省略 snl base):

    /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 
    

显示请求 header here .

  1. 输入登录名/密码后,将检索 token 并加载 protected 页面。

请求cookie如图here .

此外,我对为什么 SNL_OAUTH_TOKEN 的 token 值在上面 link (second link) 中有点困惑与我从脚本收到的 jwt token 响应中显示的内容不同。

非常感谢此处的任何指导。我也很乐意发送证明有用的任何其他非个人信息。

谢谢!

请您参考如下方法:

我最终弄明白了这一点。我低估了 Python 的请求库。

看起来就这么简单:

# prep token request data 
request_data = { 
    "client_id": client_id, 
    "client_secret": client_secret, 
    "scope": "https://www.snl.com", 
    "grant_type": "refresh_token", 
    "refresh_token": new_refresh_token 
} 
 
# post to token url with token credentials 
# the request object stores the token response cookies 
r1 = requests.post(token_url, data=request_data) 
 
# post to protected url by setting cookies arg as cookies from previous response 
r2 = requests.post(protected_url, cookies=r1.cookies)