首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用an进行授权时,出现错误` redirect_uri`缺失或无效

使用an进行授权时,出现错误` redirect_uri`缺失或无效
EN

Stack Overflow用户
提问于 2021-07-18 21:37:51
回答 1查看 229关注 0票数 0

我正在尝试使用概念API的公共集成,并严格遵循文档。链接:https://developers.notion.com/docs/authorization#authorizing-public-integrations

但是通过下面的index.html,我得到了这样的错误: rediect uri是无效的或者缺失的。

代码语言:javascript
复制
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>HTML</title>
  </head>
  <body>
    <a
      href="https://api.notion.com/v1/oauth/authorize?client_id=c7d10044-846c-423d-bc66-d6bf7838cc53&redirect_uri=https%3A%2F%2Fnotion-backend-intervue.herokuapp.com%2Fauth%2Fnotion%2Fcallback&response_type=code"
      >Add to Notion</a
    >
  </body>
</html>

为了处理所有进一步的步骤,下面是我的后端配置。

代码语言:javascript
复制
app.get("/auth/notion/callback/:code/:state", (req, res) => {
    tempCode = req.params.code;
    tempState = req.params.state;
    console.log(tempCode);
    res.send({
        "message": "Yes this is the route you are looking for",
        "code": tempCode,
        "state": tempState
    });
    axios.post('/https://api.notion.com/v1/oauth/token', {
        grant_type: "authorization_code",
        code: tempCode,
        redirect_uri: 'https://notion-backend-intervue.herokuapp.com/auth/notion/callback/'
    }).then((response) => {
        console.log(response)
    }).catch((err) => {
        console.log(`error-->`, err);
    })
});

在集成设置中,我提供了这个url作为redirect_uri:https://notion-backend-intervue.herokuapp.com/

因为这个get请求没有被调用(我从Heroku日志中确认了这一点)。我想我在提供重定向URI时遇到了一些格式化问题。请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-19 14:02:19

后来我发现这不是与重定向uri的格式有关的问题,根据文档的预期,它是正确的。但是由于一些未知的原因,文档中提供的格式将不起作用,要么是在文档之后更新了API,要么可能是由于一些bug。因此,以下是解决方案:

在锚标签中,将href链接更改为:

代码语言:javascript
复制
href="https://api.notion.com/v1/oauth/authorize?client_id=c7d10044-846c-423d-bc66-d6bf7838cc53&response_type=code"

然后,概念服务器将在重定向uri中为您提供链接,这是您在概念集成设置中提供的,在我的示例中是这样的:

代码语言:javascript
复制
https://notion-backend-intervue.herokuapp.com/auth/notion/callback

因此,在后端创建一个路由来捕获该代码,并将包含该代码的post请求发送到"https://api.notion.com/v1/oauth/token“,您将获得访问令牌作为响应。

后端路由如下所示:

代码语言:javascript
复制
app.get("/auth/notion/callback", (req, res) => {
    let tempAuthCode = req.query.code;
    console.log("Code --> ", tempAuthCode);
    axios({
        method: "post",
        url: "https://api.notion.com/v1/oauth/token",
        data: {
            "grant_type": "authorization_code",
            "code": tempAuthCode,
        },
        headers: {
            "Authorization":`Basic ${encodedToken}`,
            "Content-Type": "application/json"
        }
    }).then((response) => {
        console.log(response);
    }).catch((err) => {
        console.log(err);
    });
});

正如您在上面看到的,您还必须从post请求的数据对象中省略redirect_uri。

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

https://stackoverflow.com/questions/68429633

复制
相关文章

相似问题

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