将python会话转换为颤振会话

2024-10-05 15:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用api https://www.duolingo.com/vocabulary/overview访问我在duolingo上学到的单词。问题是,要使用此api,我必须先登录到duolingo(已验证)。我用python提出了一个解决方案,使用requests.sessions()首先将我的凭证发布到登录页面,然后在同一会话中重新路由到词汇表页面。然而,当我试图在flifter中实现这一点时,我遇到了很多问题。我能够成功地将我的凭证发布到登录页面(并获得了成功的200响应),但我无法想出如何使用post请求中的cookie/令牌成功地转到词汇表页面。 下面是我的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请求的颤振代码


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

我对Flitter非常陌生,而且我从未深入研究过会话和cookies是如何工作的,所以如果我的问题听起来很琐碎,我很抱歉


Tags: namehttpscomjsonappappledataget
1条回答
网友
1楼 · 发布于 2024-10-05 15:30:08

对于那些访问这个问题寻求答案的人,我最终还是设法找到了答案。这确实非常简单,因为我所要做的就是从post请求中保存令牌,并在get请求的头中再次传递它

以下是工作代码:

    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'],
        });

相关问题 更多 >