首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“错误”:"invalid_client“django-oauth工具包

“错误”:"invalid_client“django-oauth工具包
EN

Stack Overflow用户
提问于 2019-01-02 09:06:47
回答 2查看 5.5K关注 0票数 2

我正在使用django rest框架和django-oauth-工具箱。当我在本地主机上请求访问令牌时,它会给我访问令牌,如下所示

代码语言:javascript
复制
~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://localhost:8000/o/token/
{"access_token": "8u92BMmeZxvto244CE0eNHdLYWhWSa", "expires_in": 36000, "refresh_token": "faW06KKK71ZN74bx32KchMXGn8yjpV", "scope": "read write", "token_type": "Bearer"}

但是,当我从驻留在活动服务器上的同一个项目请求访问令牌时,它会给出与invalid_client一样的错误。

代码语言:javascript
复制
~/django_app$ curl -X POST -d "grant_type=password&username=<Your-username>&password=<your-password>" -u"<client-id>:<client-secret>" http://<your-domain>/o/token/ 
{
    "error": "invalid_client"
}

我不明白问题出在哪里。我找了很多遍,没有找到正确的答案。请告诉我该怎么做才能消除这个错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-02 11:17:19

我找到了这个问题的解决方案,而不是grant_type=password,而是使用了grant_type=client_credentials,然后我得到了访问令牌。您可以看到下面的curl命令。

代码语言:javascript
复制
curl -X POST -d "grant_type=client_credentials&client_id=<your-client id>client_secret=<your-client secret>" http://your-domain/o/token/
{"scope": "read write", "token_type": "Bearer", "expires_in": 36000, "access_token": "ITx5KCjupsdbvbKvNQFyqZDEw6svSHSfdgjh"}

如果您想用grant-type=password来完成这个任务,那么下面是命令:

代码语言:javascript
复制
curl -X POST -d "grant_type=password&username=<your-username>&password=<your-password>&client_id=<your-client id>&client_secret=<your-client secret>" http://your-domain/o/token/
{"access_token": "0BVfgujhdglxC7OHFh0we7gprlfr1Xk", "scope": "read write", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "AwffMPzNXvghlkjhs8dpXk7gbhhjhljlldfE2nI"}

我引用了这个https://developer.amazon.com/de/docs/adm/request-access-token.html,因为我的应用程序在AWS上。

票数 5
EN

Stack Overflow用户

发布于 2019-05-14 06:47:33

从JavaScript中的django-oauth工具包获取令牌:

代码语言: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());

客户端应用程序授权授予类型为:“资源所有者密码基础”。

P.S.我未能通过“Content”:"application/json"获得令牌,不知道为什么(django-oauth-工具箱文档对此只字未提)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54003651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档