简体   繁体   English

尝试使用 django-oauth-toolkit 获取访问令牌,使用 fetch 在使用 jquery 时不起作用

[英]Trying to get access token with django-oauth-toolkit using fetch not working while working using jquery

I'm trying to call endpoint to generate access token using oauth in django it is working when I call the endpoint using jquery but not working when I try to call it with fetch我正在尝试调用端点以在 django 中使用 oauth 生成访问令牌,当我使用 jquery 调用端点时它正在工作,但当我尝试使用 fetch 调用它时它不起作用

here is the code for fetch这是获取的代码

fetch(`https://<domain>/o/token/`, {
            method: 'POST',
            body:{
                grant_type:'password',
                client_id: "<client-id>",
                client_secret:"<client-secret>",
                username:"<username>",
                password:"<password>"
            }
        })
        .then(res => res.json())
        .then(res => {console.log(res)});

the output is输出是

{error: 'unsupported_grant_type'}

while when I'm calling it using jquery ajax as below its working而当我使用 jquery ajax 调用它时,它的工作如下

$.post({
            url:'https://<domain>/o/token/',
            data:{
                grant_type:'password',
                client_id: "<client-id>",
                client_secret:"<client-secret>",
                username:"<username>",
                password:"<password>"
            },
            success: function(data){
                console.log(data);
            }
        })

the output is输出是

{access_token: '<access-token>', expires_in: 3600, token_type: 'Bearer', scope: 'read write groups', refresh_token: '<refresh-token>'}

I have found the fix in case someone faced the same problem如果有人遇到同样的问题,我已经找到了解决方法

in jquery ajax as in the following code block it converts the dictionary object to query string在 jquery ajax 中,如以下代码块中所示,它将字典对象转换为查询字符串

var data = "";
            for (var x in option.data) {
                if (data != "") {
                    data += "&";
                }
                data += encodeURIComponent(x)+"="+encodeURIComponent(option.data[x]);
            };
            option.data = data;

and it sets the content-type header to并将内容类型标头设置为

'application/x-www-form-urlencoded; charset=UTF-8'

so the right fetch code that will work as the jquery ajax call will be as follow因此,将作为 jquery ajax 调用工作的正确获取代码如下

fetch(`https://<domain>/o/token/`, {
            method: 'POST',
            headers: {
                "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            body:'grant_type=password&client_id=<client-id>&client_secret=<client-secret>&username=<username>&password=<password>'
        })
        .then(res => res.json())
        .then(res => {console.log(res)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 redux 工具包并尝试更新 state 不起作用 - Using redux toolkit and trying to update the state not working 使用jQuery OAUTH检索临时访问令牌 - Retrieving Temporary Access Token using jQuery OAUTH 如何使用 puppeteer 获取 oauth 访问令牌? - How to get oauth access token using puppeteer? 使用Facebook OAuth 2.0 - 如何获取访问令牌? - Using Facebook OAuth 2.0 - How do I fetch the access token? 从Azure / oauth2 / token端点使用jQuery,Ajax或AngularJS获取access_token时出现跨域问题 - Cross-origin issue while getting access_token using jQuery, Ajax or AngularJS from Azure /oauth2/token endpoint Google OAuth 2 使用纯 Javascript 或 JQuery 的访问令牌 - Google OAuth 2 Access Token using pure Javascript or JQuery 使用oauth2的节点JS请求令牌不起作用 - Node JS requesting token using oauth2 not working Cors 在尝试使用 react/js 在客户端获取 Instagram 访问令牌时被阻止 - Cors block while trying to get Instagram access token on client side using react/js 使用npm获取令牌oAuth - Get token oAuth using npm 如何在使用 django 时验证 csrf 令牌在浏览器中是否正常运行并做出反应 - how to verify the csrf token is working well in the browser while using django and react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM