【问题标题】:Unable to use curl to get a token with Django OAuth Toolkit无法使用 curl 通过 Django OAuth Toolkit 获取令牌
【发布时间】:2014-05-03 13:54:11
【问题描述】:

我正在学习 Django 和 Django REST 框架。我想使用 oAuth 作为自动化系统。 我安装了 Django OAuth Toolkit 并注册了新的应用程序。然后,我尝试为我的应用程序获取令牌,但我无法获取它:

19:04 ~$ curl -X POST -d "grant_type=password&username=admin&password=123" http://tygkDGuF.re@HmpJB!G=oi5pHtbgxW96I9w8iTd0:zFpyRXwgUKglYAs6b=v8UwlA3qqxZr!=hn3PgzQQ5CgO=OVR-O26reO;T8xcn;xcurcbC7kPr8r-York9TIEKKEM6EFRUOhR:oU!=S3A;QQ1pmAK8dNso48KfYNgYO_F@http://127.0.0.1:8080/o/token/
bash: !G=oi5pHtbgxW96I9w8iTd0: event not found

【问题讨论】:

    标签: django bash curl oauth oauth-2.0


    【解决方案1】:

    在最后一个参数周围使用单引号:

    curl -X POST -d "grant_type=password&username=admin&password=123" 'http://tygkDGuF.re@HmpJB!G=oi5pHtbgxW96I9w8iTd0:zFpyRXwgUKglYAs6b=v8UwlA3qqxZr!=hn3PgzQQ5CgO=OVR-O26reO;T8xcn;xcurcbC7kPr8r-York9TIEKKEM6EFRUOhR:oU!=S3A;QQ1pmAK8dNso48KfYNgYO_F@http://127.0.0.1:8080/o/token/'
    

    如果不这样做,shell 会将! 解释为调用历史的运算符。而; 将被解释为命令分隔符。

    【讨论】:

      【解决方案2】:

      我用http://docs.python-requests.org/en/latest/:

      response = requests.post(
          'http://localhost:8000/o/token/',
          data={
              'grant_type': 'password',
              'username': 'you',
              'password': 'secret',
              'client_id': 'WpDs.u9yrWD3js;fYq?cog;MvTiq0Bj02r8LTL_v',
              'client_secret': 'c1W2:PzVsccsIt_G_uxnwE_TC08z14IKqYYKy0DJAok;_B?RvuVashIsARqhGwF=ChoBJveA7LvB;C?IeXyp?0ZiyBtg9;tSwTjVdC.K_f@n=K;@V;2:VoX@IhPyiHzC'
          }
      )
      
      print(response.text)
      

      【讨论】:

        【解决方案3】:

        我试过了:

        curl -X POST -d "grant_type=password&username=name&password=123" 'http://wHMBrLRjQU_!;pYIPgLGurD2SIYH.9r19!h7JSds:wCQ?KHaoVb;aS:-sqCVkkNwrwGP3B?hD9yZTGY;zFCvbR5H8Fy=mh79KYFQBtIJWza380yFF!OHeb-HyFyGtSY54oUK;_qPF4nsaAI0mt_INr.JQ0vdUgAluWPI9EeDk@127.0.0.1:8000/o/token/'
        curl: (6) Couldn't resolve host 'wHMBrLRjQU_!;pYIPgLGurD2SIYH.9r19!h7JSds'
        

        我在手册上做了所有事情 (link)

        【讨论】:

          【解决方案4】:

          就我而言,以下是工作:

          curl -X POST -d 'client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD' http://localhost:8000/o/token/
          

          注意我使用了符号'*'。

          【讨论】:

            【解决方案5】:

            还有一个从 django-oauth-toolkit 获取令牌的 Javascript 解决方案:

            async function getToken () {
                let res = await fetch("https://<your_domain>/o/token/", {
                    body: new URLSearchParams({
                        grant_type: 'password',
                        username: '<user_name>',
                        password: '<user_pass>',
                        client_id: '<client_app_id>',
                        client_secret: '<client_pass>'
                    }),
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST"
                })
                return res.json();
            }
            console.log(await getToken());
            

            您的客户端应用程序授权类型应为:“基于资源所有者密码”

            附:我无法通过 "Content-Type": "application/json" 获取令牌,不知道为什么(django-oauth-toolkit 文档对此只字未提)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-07-24
              • 2015-01-10
              • 2016-05-08
              • 2017-02-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-10
              相关资源
              最近更新 更多