python - 无法使用 python minds api 进行身份验证
问题描述
我正在尝试通过此 python api访问社交网站 minds.com,方法是在本地安装模块python3 setup.py install && pipenv run python
并按照说明登录。
但是,我在尝试进行身份验证时收到此错误消息:
(出于某种原因,Stackoverflow 不允许我发布 python 错误日志,因为它没有正确缩进,所以这里是https://pastebin.com/0sWa1hmY)
似乎是从 python api 调用的代码如下所示:
# -*- coding: utf-8 -*-
from pprint import pprint
from requests.utils import dict_from_cookiejar, cookiejar_from_dict
from minds.connections import XSRFSession
from minds.exceptions import AuthenticationError
from minds.profile import Profile
from minds.utils import add_url_kwargs
from minds.endpoints import *
from minds.sections import NewsfeedAPI, ChannelAPI, NotificationsAPI,
PostingAPI, InteractAPI
class Minds(NewsfeedAPI, ChannelAPI, NotificationsAPI, PostingAPI, ...):
_xsrf_retries = 5
def __init__(self, profile: Profile = None, login=True):
self.con = XSRFSession()
self.profile = profile
if self.profile:
if profile.cookie:
self.con.cookies = cookiejar_from_dict(profile.cookie)
if profile.proxy:
self.con.proxies = {'https': profile.proxy, 'http':\
profile.proxy}
self._establish_xsrf()
if self.profile and login and not self.is_authenticated:
if profile.username and profile.password:
self.authenticate(profile.username, profile.password)
(...)
def authenticate(self, username, password, save_profile=True) -> dict:
"""
Authenticate current instance with given user
:param: save_profile: whether to save profile locally
"""
auth = {
'username': username,
'password': password
}
resp = self.con.post(AUTHENTICATE_URL, json=auth)
self.user = resp.json()
if self.user['status'] == 'failed':
raise AuthenticationError("Couldn't log in with the ...")
self.guid = self.user['user']['guid']
if save_profile:
Profile(
username=username,
password=password,
cookie=self.get_cookies(),
proxy=self.con.proxies.get('https'),
).save()
return resp.json()
python API 似乎没有维护,但我认为 minds.com 新使用 jsonwebtokens 进行身份验证。api中缺少的东西是启用jwt的吗?
解决方案
在一次又一次地与浏览器请求进行比较之后。发生错误的原因是您提供了错误的内容类型,但实际上您需要作为 json 数据发送(似乎网站在开玩笑)。所以你需要指定headers["content-type"] = "text/plain"
. 否则网站将响应 500。
注意:为避免广泛讨论,我只能回答这个错误。
将代码更改为下面,但与源代码只有一行不同:)。
def authenticate(self, username, password, save_profile=True) -> dict:
"""
Authenticate current instance with given user
:param: save_profile: whether to save profile locally
"""
auth = {
'username': username,
'password': password
}
self.con.headers["content-type"] = "text/plain" ####
resp = self.con.post(AUTHENTICATE_URL, json=auth)
self.user = resp.json()
if self.user['status'] == 'failed':
raise AuthenticationError("Couldn't log in with the given credentials")
self.guid = self.user['user']['guid']
if save_profile:
Profile(
username=username,
password=password,
cookie=self.get_cookies(),
proxy=self.con.proxies.get('https'),
).save()
return resp.json()
推荐阅读
- javascript - 逻辑或运算符的输入文本字段问题
- sql - 如何从月表中计算月中位数?
- python - 使用 ssh 的树莓派 Python 安卓应用程序
- c# - 在服务器端为给定的 url 生成 pdf
- typo3 - 用于检查特定 list_type 是否在页面上的 TypoScript 条件
- javascript - 如何从用户输入而不是常量值开始计时器并保持我的会话存储仍在工作?
- php - PHP PDO mysql 查询同时使用 column = '$var' 和 column IS NULL
- c# - 在 Angular 中导入 excel 文件并将数据传递到 ASP.NET Core API 项目中
- python - python中的浮点数列表
- java - 自动确定在 Classpath 和 Modulepath 上放置什么?