我正在关注SODA documentation about authorization使用OAuth查询受限数据集(我的帐户有权访问)。我已经使用我的客户端密钥成功接收并存储了访问令牌。现在我正在尝试用它发出一个AJAX请求。这是我的代码:
var api_access_token = "OAuth " + stored_access_token;
jQuery.ajax({
url: "https://url/to/dataset.json",
type: "GET",
headers: {
'Authorization': api_access_token,
'X-App-Token': my_app_token
}
})
.done(function( data ) {
console.log(data);
});
但这似乎不起作用。我也尝试过使用jQuery的“beforeSend”属性,结果相同。这是我收到的:
{
"error" : true,
"message" : "You must be logged in to access this resource"
}
如果我使用cURL尝试此请求:
$headers = array(
'Content-Type: application/json',
sprintf('Authorization: OAuth %s', $access_token)
);
$curl = curl_init("https://url/to/dataset.json");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$response_data = json_decode($response, TRUE);
var_dump($response_data);
然后我得到以下(不同)错误:
"Bad OAuth token"
有什么想法吗?
修改
应用程序回调前缀:https://example.org
https://example.org/dashboard 这里我有一个锚标记,如下所示:
<a href="https://soda.demo.socrata.com/oauth/authorize?client_id=' . $app_id . '&response_type=code&redirect_uri=https://example.org/dashboard">Authorize DOIT here.</a>
这使我进入SODA页面,要求允许访问
我被重定向到这里:
https://example.org/dashboard/?code=CODE_HERE
然后服务器端脚本使用curl发布访问代码请求(如上所示),我成功接收并存储为cookie。
在同一页面(https://example.org/dashboard/?code=CODE_HERE)中,我尝试请求私有数据集,如上所述,该数据集失败。
问题是,我从https://example.org/dashboard请求授权代码,然后从https://example.org/dashboard?code=CODE_HERE查询数据
答案 0 :(得分:1)
要回答您的第一个问题,假设您尝试使用JavaScript在用户浏览器的客户端进行身份验证,即使您获得该OAuth令牌,您也无法通过CORS或JSONP使用它。为了安全起见,我们删除了跨域请求的所有身份验证标头,因为这是XSS攻击的向量。更多信息:https://dev.socrata.com/docs/cors-and-jsonp.html
至于您的PHP libcurl请求发生了什么,我不确定那里发生了什么。根据您收到的错误,您的身份验证令牌似乎已进入系统,但我的猜测是您的OAuth令牌已损坏,或者您在尝试时已过期。令牌有大约一个小时的到期时间。
使用您的OAuth令牌错误问题的答案进行修改:更改您的/oauth/authorize
网址,以反映您尝试进行身份验证的域,而不是soda.demo.socrata.com
。令牌与发布它们的域绑定。我会更新文档以使其更清晰。