将我的 python 会话变成 flutter 会话
turning my python session to a flutter session
我一直在尝试使用 api https://www.duolingo.com/vocabulary/overview 访问我在 duolingo 上学到的单词。问题是,要使用这个 api,我必须已经登录到 duolingo(经过身份验证)。我在 python 中提出了一个解决方案,使用 requests.sessions() 首先 post 我的凭据到登录页面,然后在同一会话中重新路由到词汇表页面。然而,当我试图在 Flutter 中实现这个时,我遇到了很多问题。我能够成功 post 我的登录页面凭据(并获得成功 200 响应),但我无法弄清楚如何使用 post 请求中的 cookies/tokens成功进入词汇页面。
下面是我的工作 python 代码(为了安全删除了用户名和密码)
import requests
import json
import ast
headers = {'content-type': 'application/json'}
data = {
'identifier': 'username',
'password': 'password',
}
with requests.Session() as s:
url = "https://www.duolingo.com/2017-06-30/login?fields="
# use json.dumps to convert dict to serialized json string
s.post(url, headers=headers, data=json.dumps(data))
r = s.get("https://www.duolingo.com/vocabulary/overview")
cont = r.content.decode('utf-8')
print(cont)
这里是带有 post 请求和非工作 get 请求的 flutter 代码
final response = await http.post(
Uri.parse('https://www.duolingo.com/2017-06-30/login?fields='),
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(<String, String>{
'identifier': 'username',
'password': 'password',
}),
); // the post returns a success 200 message
if (response.statusCode == 200) {
print("success");
final resp2 = await http.get(
Uri.parse("https://www.duolingo.com/vocabulary/overview"),
); //the get request is supposed to return a json with all my learned words but is instead returning html of the "404 error page)
}
这里是 get 请求的输出
<!doctype html><html dir="ltr"><head><title>Duolingo</title><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"><meta name="robots" content="NOODP"><meta name="theme-color" content="#eeeeee"><noscript><meta http-equiv="refresh" content="0; url=/nojs/splash"></noscript><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="apple-mobile-web-app-title" content="Duolingo"><meta name="google" content="notranslate"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-itunes-app" content="app-id=570060128"><meta name="facebook-domain-verification" content="mwudgypvvgl4fekxjk5rpk3eqg7ykt"><link rel="apple-touch-icon" href="//d35aaqx5ub95lt.cloudfront.net/images/duolingo-touch-icon2.png"><link rel="shortcut icon" type="image/x-icon" href="//d35aaqx5ub95lt.cloudfront.net/favicon.ico"><script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" data-document-langua
我是 Flutter 的新手,我从未深入研究会话和 cookie 的工作原理,所以如果我的问题听起来微不足道,我深表歉意。
对于那些访问该问题寻找答案的人,我最终确实设法找到了它。这确实非常简单,因为我所要做的就是从 post 请求中保存令牌,然后在获取请求
的 header 中再次传递它
这是工作代码:
var header
final response = await http.post(
Uri.parse('https://www.duolingo.com/2017-06-30/login?fields='),
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(<String, String>{
'identifier': 'username',
'password': 'password',
}),
);
if (response.statusCode == 200) {
header = response.headers;
}
final resp2 = await http.get(
Uri.parse("https://www.duolingo.com/vocabulary/overview"),
headers: {
HttpHeaders.authorizationHeader: header['jwt'],
});
我一直在尝试使用 api https://www.duolingo.com/vocabulary/overview 访问我在 duolingo 上学到的单词。问题是,要使用这个 api,我必须已经登录到 duolingo(经过身份验证)。我在 python 中提出了一个解决方案,使用 requests.sessions() 首先 post 我的凭据到登录页面,然后在同一会话中重新路由到词汇表页面。然而,当我试图在 Flutter 中实现这个时,我遇到了很多问题。我能够成功 post 我的登录页面凭据(并获得成功 200 响应),但我无法弄清楚如何使用 post 请求中的 cookies/tokens成功进入词汇页面。 下面是我的工作 python 代码(为了安全删除了用户名和密码)
import requests
import json
import ast
headers = {'content-type': 'application/json'}
data = {
'identifier': 'username',
'password': 'password',
}
with requests.Session() as s:
url = "https://www.duolingo.com/2017-06-30/login?fields="
# use json.dumps to convert dict to serialized json string
s.post(url, headers=headers, data=json.dumps(data))
r = s.get("https://www.duolingo.com/vocabulary/overview")
cont = r.content.decode('utf-8')
print(cont)
这里是带有 post 请求和非工作 get 请求的 flutter 代码
final response = await http.post(
Uri.parse('https://www.duolingo.com/2017-06-30/login?fields='),
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(<String, String>{
'identifier': 'username',
'password': 'password',
}),
); // the post returns a success 200 message
if (response.statusCode == 200) {
print("success");
final resp2 = await http.get(
Uri.parse("https://www.duolingo.com/vocabulary/overview"),
); //the get request is supposed to return a json with all my learned words but is instead returning html of the "404 error page)
}
这里是 get 请求的输出
<!doctype html><html dir="ltr"><head><title>Duolingo</title><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"><meta name="robots" content="NOODP"><meta name="theme-color" content="#eeeeee"><noscript><meta http-equiv="refresh" content="0; url=/nojs/splash"></noscript><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="apple-mobile-web-app-title" content="Duolingo"><meta name="google" content="notranslate"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-itunes-app" content="app-id=570060128"><meta name="facebook-domain-verification" content="mwudgypvvgl4fekxjk5rpk3eqg7ykt"><link rel="apple-touch-icon" href="//d35aaqx5ub95lt.cloudfront.net/images/duolingo-touch-icon2.png"><link rel="shortcut icon" type="image/x-icon" href="//d35aaqx5ub95lt.cloudfront.net/favicon.ico"><script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" data-document-langua
我是 Flutter 的新手,我从未深入研究会话和 cookie 的工作原理,所以如果我的问题听起来微不足道,我深表歉意。
对于那些访问该问题寻找答案的人,我最终确实设法找到了它。这确实非常简单,因为我所要做的就是从 post 请求中保存令牌,然后在获取请求
的 header 中再次传递它这是工作代码:
var header
final response = await http.post(
Uri.parse('https://www.duolingo.com/2017-06-30/login?fields='),
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(<String, String>{
'identifier': 'username',
'password': 'password',
}),
);
if (response.statusCode == 200) {
header = response.headers;
}
final resp2 = await http.get(
Uri.parse("https://www.duolingo.com/vocabulary/overview"),
headers: {
HttpHeaders.authorizationHeader: header['jwt'],
});