我尝试使用PHP和JQuery构建一个与Pocket接口的应用程序
按照此处的步骤http://getpocket.com/developer/docs/authentication我坚持"步骤2:获取请求令牌"
文档告诉我:
POST /v3/oauth/request HTTP/1.1
Host: getpocket.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Accept: application/x-www-form-urlencoded
consumer_key=1234-abcd1234abcd1234abcd1234&
redirect_uri=pocketapp1234:authorizationFinished
这对我没有意义。如何格式化此OAuth JSON POST请求以使用PHP和/或JQuery?
注意,我尝试使用https://github.com/jshawl/pocket-oauth-php,但它在回调上失败,并且在文档明确指出POST时似乎使用了GET。我也想自己解决这个问题,这样我就可以学习如何将OAuth用于其他API。
谢谢。
答案 0 :(得分:3)
这似乎工作正常:
<?php
session_start();
$redirect_uri = 'http://path/to/app/callback';
$url = 'https://getpocket.com/v3/oauth/request';
$data = array('consumer_key' => '*****', 'redirect_uri' => $redirect_uri);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded; charset=UTF-8",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$request_token = str_replace('code=','',$result);
$_SESSION['request_token'] = $request_token;
header("Location: https://getpocket.com/auth/authorize?request_token=$request_token&redirect_uri=$redirect_uri");
die();
?>
之后我在会话中使用request_token以类似的方式获取访问令牌。