【问题标题】:not a valid key=value pair (missing equal-sign) in Authorization header授权标头中不是有效的键=值对(缺少等号)
【发布时间】:2021-08-31 17:52:53
【问题描述】:

从 Postman 访问 API 时出现此错误。

API 详细信息:

网址:

https://account-perf.myglobal.com/v1/users/00uk0khprrME7gZOU0h7/credentials/change_password

标题:

Content-Type:application/json
Authorization:Bearer n7mbkw74jsubd7rauhptdnre

类型:

发布

正文:

{"password":"Baddy125@","token":"eyJhbGci...."}

编辑 1:

Web 服务调用以生成令牌-

网址-

https://api-perf.myglobal.com/rest/oauth2/v1/token

类型-

发布

身体-

client_id:abcd
client_secret:xyz
grant_type:client_credentials

【问题讨论】:

  • n7mbkw74jsubd7rauhptdnre 你是如何产生这个值的?

标签: web-services authentication post oauth postman


【解决方案1】:

每当调用任何未处理的端点方法或资源时,我都会遇到此问题。我的设置是一个 API 网关,具有定义的资源(例如 /myendpoint)和为这些端点定义的方法(例如 GET)。

为了解决这个问题,我创建了一个 Node.js Lambda 函数,它只返回了 404。然后我在端点 / 的根处添加了任何 ANY 方法,并将其作为 Lambda 代理函数指向 ANY 方法.

然后我添加了一个代理资源,例如/{proxy} -- 创建资源时可以单击一个复选框来告诉它代理。该资源上的 ANY 方法指向同一个 Lambda 函数,部署 API,我就完成了。

现在我得到一个正确的HTTP 404 错误,而不是身份验证持有者令牌错误。

【讨论】:

    【解决方案2】:

    @Matt H - 这是个好主意,这给了我另一个灵感。

    假设 API 中的所有其他路径都已明确指定,我创建了一个默认路径 /{proxy+},它将返回 http 404,找不到消息资源。我没有使用 lambda,而是能够创建一个模拟响应,因此让 Lambda 返回响应甚至不会产生任何成本。

    我通过 Open API 规范创建了我的 API。这就是我的 YAML 实现方式

      /{proxy+}:
        x-amazon-apigateway-any-method:
          responses:
            404:
              description: "404 response"
              content: {}
          x-amazon-apigateway-integration:
            responses:
              404:
                statusCode: "404"
                responseTemplates:
                  application/json: "{\"message\":\"resource not available\"}"
            requestTemplates:
              application/json: "{\"statusCode\": 404}"
            passthroughBehavior: "when_no_templates"
            type: "mock"
    

    无服务器还可以指定内联模拟响应。下面可以是一个示例:

    functions:
      default:
        handler: handler.default
        events:
          - http:
              path: hello
              cors: true
              method: get
              integration: mock
              request:
                template:
                  application/json: '{"statusCode": 404}'
              response:
                template: $input.path('$')
                statusCodes:
                  404:
                    pattern: '' #default method
                    template:
                      application/json: '{"statusCode": 404, "message":"resource not found"}'
    

    无服务器文档:https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#custom-response-templates

    【讨论】:

      【解决方案3】:

      分析并验证请求路径,如果请求不正确,API 网关会抛出此错误。我踩到了同样的错误,当更正请求参数时它运行良好。

      告诉我。

      【讨论】:

        【解决方案4】:

        我设法通过将 ANY /{proxy+} 路由代理到一个 lambda 来做到这一点,该 lambda 总是以 HTTP 404 响应

        由于所有其他路由都经过精确配置,因此ANY /{proxy+} 路由作为默认路由,将捕获任何不匹配的请求

        这是我使用 CloudFormation 的方法:

        Parameters:
            RestAPI:
                Type: String
            RestApiRootResourceId:
                Type: String
            LambdaName:
                Type: String
            Path:
                Type: String
        
        RootResource:
            Type: AWS::ApiGateway::Resource
            Properties:
              RestApiId: !Ref RestAPI
              ParentId: !Ref RestApiRootResourceId
              PathPart: !Ref Path
        ProxyResource:
            Type: 'AWS::ApiGateway::Resource'
            Properties:
              RestApiId: !Ref RestAPI
              ParentId: !Ref RestApiRootResourceId
              PathPart: "{proxy+}"
        AnyMethod:
            Type: 'AWS::ApiGateway::Method'
            Properties:
            RestApiId: !Ref RestAPI
            ResourceId: !Ref ProxyResource
            HttpMethod: ANY
            Integration:
                IntegrationHttpMethod: POST
                Type: AWS_PROXY
                PassthroughBehavior: WHEN_NO_MATCH
                Uri:
                  Fn::Join:
                    - ":"
                    - - !Sub "arn:aws:apigateway:${AWS::Region}:lambda"
                      - !Sub "path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function"
                      - !Sub "${LambdaName}/invocations"
        ApiGatewayInvokeLambdaPermissionAny:
            Type: AWS::Lambda::Permission
            Properties:
              Action: lambda:InvokeFunction
              FunctionName:
                Fn::Join:
                  - ":"
                  - - !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function"
                    - !Ref LambdaName
              Principal: apigateway.amazonaws.com
              SourceArn:
                Fn::Join:
                  - ":"
                  - - !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}"
                    - !Sub "${RestAPI}/*/ANY/*"
        

        参数:

        • RestAPI 是您的 API ID
        • RestApiRootResourceId 是根资源的 ID (!GetAtt "RestApi.RootResourceId")
        • LambdaName 是您的代理 lambda 的名称
        • 路径是我在阶段和根之间的东西(用于 mty 特定用途)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-16
          • 2019-12-01
          • 1970-01-01
          • 2012-10-27
          • 1970-01-01
          • 2021-06-23
          • 1970-01-01
          相关资源
          最近更新 更多