【问题标题】:Make WordPress API Call Using RCurl/HTTR使用 RCurl/HTTR 进行 WordPress API 调用
【发布时间】:2016-08-08 19:53:02
【问题描述】:

我希望通过 API 调用来获取受保护的 WP 博客的帖子数据。我在将 WP 身份验证过程从 PHP 转换为 R 时遇到了一些问题,我认为部分原因是我不完全理解该过程。

我对OAuth令牌接收流程的理解来自this page

  1. 将用户发送到提示登录的授权端点: https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_url&response_type=code&blog=1234
  2. 然后在 API 上发出 POST 请求,并包含上述重定向 url 中的代码:

$curl = curl_init('https://public-api.wordpress.com/oauth2/token'); curl_setopt( $curl, CURLOPT_POST, true ); curl_setopt( $curl, CURLOPT_POSTFIELDS, array( 'client_id' => your_client_id, 'redirect_uri' => your_redirect_url, 'client_secret' => your_client_secret_key, 'code' => $_GET['code'], // The code from the previous request 'grant_type' => 'authorization_code' ) ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1); $auth = curl_exec( $curl ); $secret = json_decode($auth); $access_key = $secret->access_token;

然后返回:

{
    "access_token": "YOUR_API_TOKEN",
    "blog_id": "blog ID",
    "blog_url": "blog url",
    "token_type": "bearer"
}

但我不知道从哪里开始 r。我很抱歉这不是一个可重现的问题。我尝试使用代码here,但我无法完全弄清楚出了什么问题:

app_name <- 'myapp' 
client_id <- 'your_client_id'
redirect_uri <- 'your_redirect_url'
client_secret <- 'your_client_secret_key'
resource_uri <- #IDK what this is

oauth_endpoint(authorize = "https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_url&response_type=code&blog=1234",
access = "https://public-api.wordpress.com/oauth2/token")
wordpress_endpoint <- oauth_endpoints('wordpress')

# Create the app instance.
myapp <- oauth_app(key = 'your_client_id',
                   secret = 'your_client_secret_key')

mytoken <- oauth2.0_token(wordpress_endpoint, myapp,
                          user_params = list(resource = resource_uri),
                          use_oob = FALSE)

【问题讨论】:

  • 在网上搜索了使用httr 对各种API 进行身份验证的示例并将一些最终无法正常工作的代码放在一起后,我理解了被卡住的挫败感。请发布您迄今为止尝试过的代码,以便我们为您提供帮助。
  • 所以我已经用我迄今为止的尝试更新了这个问题,但我不太确定我是否理解httr。您是否必须使用包中的“预加载”API 之一(例如, ouath_endpoints("google"))?

标签: r wordpress curl oauth httr


【解决方案1】:

某些 API 有内置端点...因此您不必自己获取 URL。我不认为 wordpress 是其中之一,所以这就是您通过oauth_endpoint 调用为自己创建的内容。像这样的东西可能会起作用......但我没有一个 wordpress 帐户来测试它......

wordpress <- oauth_endpoint(authorize = "https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_url&response_type=token&blog=1234",
                            access = "https://public-api.wordpress.com/oauth2/token")

myapp <- oauth_app(key = 'your_client_id',
                   secret = 'your_client_secret_key')

mytoken <- oauth2.0_token(wordpress, myapp)

【讨论】:

  • 因此,当我在 R 中运行该代码时,它会显示此消息 Invalid request, please go back and try again. Error Code: invalid_request Error Message: Mismatch in redirect_uri. 但是当我在浏览器中运行授权地址时,它会将我带到 WP 授权页面...
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多