如何在Mongoatlas GraphQl API中使用JWT标准

发布于 2025-02-10 10:27:11 字数 433 浏览 1 评论 0 原文

我正在尝试配置我的codegen.yml文件,以与mongodb地图集上的GraphQl API进行交互。但是,当我使用授权时:“ evazilp8lgifjivrqdgolsjqbqm8kh5w2ykd4gd1hhr4fq4fq4dbe2i4wtetm21hmpv“ ” 当我使用授权时:“携带者EVAZILP8LGIFJIVRQDGOLSJQBQM8KH5W2YKD4GD1HHR4FQ4DBE2BE2I4WTETM21HMPV” 输入/ rel =“ nofollow noreferrer”> https://www.mongodb.com/docs/atlas/app-services/graphql/authenticate/ ,我得到 token包含无效的段数 。我该如何解决?因为我从字面上逐步遵循文档,现在当我完全做它说的事情时,我会遇到错误。

I'm trying to configure my codegen.yml file to interact with my GraphQL API on the MongoDB Atlas. But when I use Authorization: "eVaziLp8LgiFJivRQDgoLsjqbqm8KH5W2ykd4GD1hhR4FQ4DbE2I4wtEtM21hmpv", I receive the error expected Authorization header with JWT (Bearer schema).
And when I use Authorization: "Bearer eVaziLp8LgiFJivRQDgoLsjqbqm8KH5W2ykd4GD1hhR4FQ4DbE2I4wtEtM21hmpv" as the docs suggest: https://www.mongodb.com/docs/atlas/app-services/graphql/authenticate/, I get token contains an invalid number of segments. How can I solve this? Because I'm literally following the documentation step by step, and now when I do exactly what it says, I get an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-17 10:27:11

您在承载后包含的那个字符串 evaz ... 看起来不像是正确格式化的JWT,它的3个片段分别为时期。

您需要使用一个领域SDK或Admin API获取客户端访问令牌。

这里是有关如何使用Web SDK获取访问令牌的文档 -

我认为所有其他领域客户端SDK都应该具有 user.accesstoken conscotor方法。

以及有关如何使用Admin API获取访问令牌的更多一般文档 - https://www.mongodb.com/docs/atlas/atlas/app-services/reference/reference/reference/authenticate/authenticate-http-client-reqlient-request-requests/#std -label-authenticate-http-client-requests

另外,如果您使用的是自定义JWT auth,请求标题不同。从文档中:

http.post({
  "url": "https://realm.mongodb.com/api/client/v2.0/app/<yourappid-abcde>/graphql",
  "headers": {
    "jwtTokenString": "<User's JWT Token>"
  },
  "body": '{"query":"query AllMovies {\n  movies {\n    title\n    year\n  }\n}"}'
})

在自定义JWT请求标头上进行进一步阅读 - https://www.mongodb.com/docs/atlas/app-services/graphql/authenticate/#custom-jwt

That string which you've included after Bearer eVaz... doesn't look like a properly formatted JWT, which has 3 segments separated by periods.

You need to get the client access token using one of the Realm SDKs or the Admin API.

Here's docs on how to get the access token with the Web SDK - https://www.mongodb.com/docs/realm/web/graphql-apollo-react/

I think all the other Realm client SDKs should also have a User.accessToken accessor method.

And more general documentation on how to use the Admin API to get an access token - https://www.mongodb.com/docs/atlas/app-services/reference/authenticate-http-client-requests/#std-label-authenticate-http-client-requests

Alternatively, if you're using Custom JWT auth, the request header is different. From the docs:

http.post({
  "url": "https://realm.mongodb.com/api/client/v2.0/app/<yourappid-abcde>/graphql",
  "headers": {
    "jwtTokenString": "<User's JWT Token>"
  },
  "body": '{"query":"query AllMovies {\n  movies {\n    title\n    year\n  }\n}"}'
})

Further reading on custom JWT request headers - https://www.mongodb.com/docs/atlas/app-services/graphql/authenticate/#custom-jwt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文