我在使用OAuth查询受限制的数据集(即我的帐户有权访问)的SODA documentation about authorization。我已经使用我的客户端密钥成功接收和存储访问令牌。现在我试图用它做出一个AJAX请求。这是我的代码:验证访问受限制的SODA数据集的请求
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>
这让我来,要求允许访问苏打页
- 获得访问令牌
- 使用访问令牌
我在这里重定向:
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
你对该域名发出了哪个域名?您必须针对与您使用该标记相同的域名使用OAuth。我认为这可能是问题所在。 – chrismetcalf
用我正在使用的域和流编辑我的问题。 – duncanrager
是的,改变你的'/ oauth/authorize' URL来反映你想要认证的域名,而不是'soda.demo.socrata.com'。令牌与他们签发的域名绑定。我会更新文档以使其更清楚。 – chrismetcalf