【问题标题】:405 method not allowed error in AWS Cognito oauth2/token endpointAWS Cognito oauth2/token 端点中的 405 方法不允许错误
【发布时间】:2019-09-12 00:23:24
【问题描述】:

我正在使用 AWS Cognito UI 使用授权代码授予流程登录并成功获取授权代码。但是当通过 postman 向 oauth2/token 端点发出 post 请求时出现 405 method not allowed 错误

应用程序客户端设置在 Cognito 用户池中,应用程序密钥通过 appclientid:appclientsecret 作为 base64 编码的授权。

【问题讨论】:

    标签: amazon-web-services oauth-2.0 amazon-cognito


    【解决方案1】:

    如文档中所述:

    内容类型 必须始终为“application/x-www-form-urlencoded”。

    来源:https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

    【讨论】:

      【解决方案2】:

      使用 BasicAuth 的 Authentication 并提供Username=client_id, Password=client_secret

      使用POST方法

      使用Body = x-www-form-urlencoded

      不要忘记在 Body 中也使用 State 值。

      【讨论】:

        【解决方案3】:

        我遇到了类似的问题。就我而言,我必须将 Accept 标头更改为 */*

        当我将其命名为 Accept=text/html,application/xhtml+xml,application/xml 时,它以 405 响应 /token 端点。希望这对某人有所帮助。

        【讨论】:

          【解决方案4】:

          我在 c# 中为授权类型的令牌编写代码,所有调用都以 405 Method Not Allowed 状态失败。

          根据 AWS 文档,应使用以下 URL 和参数

          POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token&
          Content-Type='application/x-www-form-urlencoded'&
          Authorization=Basic aSdxd892iujendek328uedj
          
          grant_type=authorization_code&
          client_id=djc98u3jiedmi283eu928&
          code=AUTHORIZATION_CODE&
          redirect_uri=com.myclientapp://myclient/redirect
          

          花了 2 个小时后,我发现从 URL 中删除 & 可以解决问题,所以请确保您的请求看起来像这样

          POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token
          Content-Type='application/x-www-form-urlencoded'
          Authorization=Basic aSdxd892iujendek328uedj
          
          grant_type=authorization_code&
          client_id=djc98u3jiedmi283eu928&
          code=AUTHORIZATION_CODE&
          redirect_uri=com.myclientapp://myclient/redirect
          

          【讨论】:

            【解决方案5】:

            我通过使我的代码如下所述解决了 AWS Cognito oauth2/token 端点中的此错误 405 方法不允许错误,并且它运行良好。 我从这个链接中获得了帮助,并使用正确的格式在 fetch 请求中提到了 header 和 body 参数:

            https://formcarry.com/documentation/fetch-api-example

              const requestOptions = {
                method: "POST",
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                  "Authorization": `Basic ${authData}`,
                  "Accept": "application/json"            
                },
                body: `grant_type=${config.grant_type}&code=${code}&client_id=${config.clientId}&redirect_uri=${config.loginRedirectUri}`
              }
                    
              fetch(`${config.domainUrl}/oauth2/token`, requestOptions)
                .then(response => response.json())
                .then(data => {
                  sessionStorage.setItem("access_token",data.access_token)
                  fetchUserDetails(data.access_token)
                })
            

            我使用配置文件来保存变量。

            const config = {
              domainUrl: "https://domainname.auth.origin.amazoncognito.com",
              clientId: "xxxxxxxxxxxx",
              loginRedirectUri: "http://localhost:8000/redirecturi",
              grant_type: "authorization_code",
              logoutUri: "http://localhost:8000",
              clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            }
            

            【讨论】:

              【解决方案6】:
                      var strClientSecret = $"{"your_clientId"}:{"your_clientsecret"}";
                      var client = new HttpClient();
                      var body = new Dictionary<string, string>();
                      body.Add("grant_type", "client_credentials");
                      body.Add("client_id", "your_appclientid");
                      body.Add("redirect_uri", "your_callbackurl");
              
                      var content = new FormUrlEncodedContent(body);
                      var autho = System.Text.Encoding.UTF8.GetBytes(strClientSecret);
                      var base64Autho = System.Convert.ToBase64String(autho);
                      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Autho);
              
                      client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
              
                      var response = await client.PostAsync("https://your_domain.auth.ap-south-1.amazoncognito.com/oauth2/token", content);
              

              【讨论】:

                【解决方案7】:

                好吧,以防万一它对任何人有帮助。

                在尝试检索相应的邮件时,我在 Postman 中遇到了 405 jwt 令牌(id_token、access_token、refresh_token)使用 grant_type 作为授权码。

                原因是我使用 'application/x-www-form-urlencoded' 作为 Content-Type 的值的标题部分,即使用单引号。因此,当我删除这些单引号并立即使用 application/x-www-form-urlencoded 时,它就开始工作了。

                【讨论】:

                  猜你喜欢
                  • 2016-05-20
                  • 2017-11-13
                  • 1970-01-01
                  • 2021-12-20
                  • 2019-09-03
                  • 2018-06-15
                  • 2016-12-02
                  • 2015-11-25
                  • 2012-10-25
                  相关资源
                  最近更新 更多