【发布时间】:2016-08-08 19:53:02
【问题描述】:
我希望通过 API 调用来获取受保护的 WP 博客的帖子数据。我在将 WP 身份验证过程从 PHP 转换为 R 时遇到了一些问题,我认为部分原因是我不完全理解该过程。
我对OAuth令牌接收流程的理解来自this page:
- 将用户发送到提示登录的授权端点:
https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_url&response_type=code&blog=1234 - 然后在 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