【问题标题】:turning my python session to a flutter session将我的 python 会话转换为颤动会话
【发布时间】:2021-09-16 05:51:52
【问题描述】:

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

我是 Flutter 的新手,我从未深入研究过会话和 cookie 的工作原理,所以如果我的问题听起来微不足道,我深表歉意。

【问题讨论】:

    标签: python flutter session cookies get


    【解决方案1】:

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

    这是工作代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 2012-01-16
      • 2020-08-10
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      相关资源
      最近更新 更多