You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中调用Inoreader API出错,求帮助实现OAuth2.0令牌获取函数

修复Inoreader OAuth2令牌获取函数的问题

你的代码存在几个关键错误导致URL格式异常,以下是修正方案:

核心问题分析

  • URL格式错误:POST()的第一个参数需传入完整API端点URL,不能附加HTTP/1.1,正确的令牌端点是https://www.inoreader.com/oauth2/token
  • 冗余请求头:Host和Content-length无需手动设置,httr会自动处理这些HTTP标准头
  • 缺失必填参数:OAuth2令牌请求必须包含grant_type,需根据你选用的授权流程指定对应值(如授权码流程用authorization_code,密码流程用password)

修正后的代码

library(httr)
library(jsonlite)

get_inoreader_token <- function(client_id, client_secret, redirect_uri, auth_code = NULL, username = NULL, password = NULL) {
  # 根据授权流程选择对应的grant_type及参数
  if (!is.null(auth_code)) {
    grant_type <- "authorization_code"
    body_params <- list(
      client_id = client_id,
      client_secret = client_secret,
      redirect_uri = redirect_uri,
      grant_type = grant_type,
      code = auth_code
    )
  } else if (!is.null(username) && !is.null(password)) {
    grant_type <- "password"
    body_params <- list(
      client_id = client_id,
      client_secret = client_secret,
      redirect_uri = redirect_uri,
      grant_type = grant_type,
      username = username,
      password = password,
      scope = "read"
    )
  } else {
    stop("请提供授权码(authorization_code流程)或用户名密码(password流程)")
  }
  
  res <- POST(
    url = "https://www.inoreader.com/oauth2/token",
    add_headers(
      `User-Agent` = "your-custom-user-agent"  # 替换为你的应用标识
    ),
    body = body_params,
    encode = "form"  # 自动适配application/x-www-form-urlencoded格式
  )
  
  stop_for_status(res)
  content(res, "parsed")  # 直接解析JSON响应内容
}

使用说明

  1. 授权码流程(推荐用于公共应用):

    • 先引导用户访问Inoreader授权页面获取授权码:https://www.inoreader.com/oauth2/auth?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code&scope=read
    • 调用函数时传入授权码:get_inoreader_token(client_id = "你的客户端ID", client_secret = "你的客户端密钥", redirect_uri = "你的回调地址", auth_code = "获取到的授权码")
  2. 密码流程(仅适合个人测试/私有应用):

    • 直接传入账号信息:get_inoreader_token(client_id = "你的客户端ID", client_secret = "你的客户端密钥", redirect_uri = "你的回调地址", username = "你的邮箱", password = "你的密码")

注意事项

  • 需先在Inoreader开发者后台注册应用,获取client_id和client_secret
  • redirect_uri必须与开发者后台设置的地址完全一致
  • 密码流程存在安全风险,不建议用于面向公众的应用

内容的提问来源于stack exchange,提问作者Gion Mors

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:05:36