AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78566573
Accepted
johnf42
johnf42
Asked: 2024-06-02 21:32:04 +0800 CST2024-06-02 21:32:04 +0800 CST 2024-06-02 21:32:04 +0800 CST

OAuth2 无效授权

  • 772

我在 Julia 工作,并尝试连接到 Schwab API。这是我的代码:

authUrl = "https://api.schwabapi.com/v1/oauth/authorize?client_id=$clientID&redirect_uri=https://127.0.0.1"
;

println(authUrl)

println("redirect url")
returnedLink = readline()
;

code = returnedLink[findfirst("code=", returnedLink).stop + 1:findfirst("%40", returnedLink).start - 1] * "@";

encode = base64encode("$clientID:$secret")

headers = Dict(
    "Authorization" => "Basic $encode",
    "Content-Type" => "application/x-www-form-urlencoded"
)

data = [
    "grant_type" => "authorization_code",
    "code" => code,
    "redirect_uri" => "https://127.0.0.1"
]
;

response = HTTP.post("https://api.schwabapi.com/v1/oauth/token", headers=headers, form=data)

它给了我这个错误消息:

HTTP.Exceptions.StatusError(400, "POST", "/v1/oauth/token", HTTP.Messages.Response:
"""
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 98
Cache-Control: no-store
Pragma: no-cache
Expires: -1
Access-Control-Allow-Headers: *, X-Authorization
Access-Control-Max-Age: 3628800
Access-Control-Allow-Methods: GET, POST, OPTIONS
Date: Sun, 02 Jun 2024 13:28:57 GMT
Connection: close
Strict-Transport-Security: max-age=30


            {"error":"invalid_grant","error_description":"Missing parameter grant_type"}
        """)

即使我在上面指定了授权类型,为什么还是会收到无效授权错误?

http
  • 1 1 个回答
  • 22 Views

1 个回答

  • Voted
  1. Best Answer
    Subham Kumar sinha
    2024-06-02T21:58:19+08:002024-06-02T21:58:19+08:00

    您似乎正在尝试使用 Julia 连接到 Schwab API,但遇到了 OAuth 令牌交换问题。该错误消息表明缺少参数,特别是grant_type,即使您已在请求中指定了该参数。

    以下是该问题的详细说明以及解决该问题的一些步骤:

    问题解释

    HTTP.Exceptions.StatusError(400, "POST", "/v1/oauth/token", HTTP.Messages.Response:
    """
    HTTP/1.1 400 Bad Request
    Content-Type: application/json
    Content-Length: 98
    Cache-Control: no-store
    Pragma: no-cache
    Expires: -1
    Access-Control-Allow-Headers: *, X-Authorization
    Access-Control-Max-Age: 3628800
    Access-Control-Allow-Methods: GET, POST, OPTIONS
    Date: Sun, 02 Jun 2024 13:28:57 GMT
    Connection: close
    Strict-Transport-Security: max-age=30
    
    {"error":"invalid_grant","error_description":"Missing parameter grant_type"}
    """)
    

    这表明grant_type您的 POST 请求中的参数未被识别。发生这种情况的原因有多种,例如请求数据或标头的格式不正确。

    潜在问题和解决方案

    1. data参数格式:确保data参数格式正确为 URL 编码形式。在 Julia 中,使用成对数组可能无法正确转换为预期的表单数据格式。

    2. 标头:确保标头设置正确并且Content-Type确实如此application/x-www-form-urlencoded。

    更正代码

    这是代码的更正版本,并附有详细说明:

    using HTTP
    using Base64
    
    # Define your clientID and secret
    clientID = "your_client_id"
    secret = "your_secret"
    
    # Construct the authorization URL
    authUrl = "https://api.schwabapi.com/v1/oauth/authorize?client_id=$clientID&redirect_uri=https://127.0.0.1"
    println(authUrl)
    
    # Print the redirect URL and read the returned link
    println("redirect url")
    returnedLink = readline()
    
    # Extract the authorization code from the returned link
    code_start = findfirst("code=", returnedLink).stop + 1
    code_end = findfirst("%40", returnedLink).start - 1
    code = returnedLink[code_start:code_end] * "@"
    
    # Encode clientID and secret for basic authentication
    encode = base64encode("$clientID:$secret")
    
    # Set the headers
    headers = Dict(
        "Authorization" => "Basic $encode",
        "Content-Type" => "application/x-www-form-urlencoded"
    )
    
    # Set the form data
    data = Dict(
        "grant_type" => "authorization_code",
        "code" => code,
        "redirect_uri" => "https://127.0.0.1"
    )
    
    # Convert form data to URL-encoded format
    encoded_data = HTTP.URIs.querystring(data)
    
    # Make the POST request
    response = HTTP.post("https://api.schwabapi.com/v1/oauth/token", headers=headers, body=encoded_data)
    
    # Print the response
    println(String(response.body))
    

    主要变化

    1. 使用Dictfordata:data从对数组更改为 aDict以确保正确处理。

    2. 对表单数据进行 URL 编码:用于HTTP.URIs.querystring(data)对表单数据进行正确编码。

    3. body在 中设置HTTP.post参数: 用于body=encoded_data而不是form=data确保数据以 URL 编码形式发送。

    通过进行这些调整,您的 OAuth 令牌请求的格式应该正确,并且您应该不会再遇到“invalid_grant”错误。如果您仍然遇到问题,接下来的步骤是验证 Schwab API 的确切要求并确保所有参数都得到正确处理。

    • 2

相关问题

  • 浏览器何时拒绝 CORS 请求?

  • 一些大型 API 提供商实际上如何执行白名单域?

  • 如何仅使用http crate 发送http 请求?

  • 使用 HTTP 1.1 的服务器如何理解使用 HTTP 2 的客户端?

  • Rust reqwest::multipart::Form 不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve