0

我正在关注关于使用 OAuth 来查询受限数据集(我的帐户有权访问)的授权的 SODA 文档。我已使用我的客户端密码成功接收并存储了访问令牌。现在我正在尝试用它发出 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

  1. 获取授权码

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 页面

  1. 获取访问令牌

我被重定向到这里:

https://example.org/dashboard/?code=CODE_HERE

然后服务器端脚本使用 curl 发布访问代码请求(如上所示),我成功接收并存储为 cookie。

  1. 使用访问令牌

从同一页面(https://example.org/dashboard/?code=CODE_HERE)我尝试请求私有数据集,但如上所述失败。

是这个问题吗,我从 https://example.org/dashboard 请求授权码,然后从https://example.org/dashboard?code=CODE_HERE查询数据

4

1 回答 1

1

要回答您的第一个问题,假设您尝试使用 JavaScript 在用户浏览器中的客户端进行身份验证,即使您获得了该 OAuth 令牌,您也无法通过 CORS 或 JSONP 使用它。为了安全起见,我们删除了跨域请求的所有身份验证标头,因为这是 XSS 攻击的载体。更多信息:https ://dev.socrata.com/docs/cors-and-jsonp.html

至于您的 PHP libcurl 请求发生了什么,我不确定那里发生了什么。根据您收到的错误,您的身份验证令牌似乎正在进入系统,但我的猜测是您的 OAuth 令牌已损坏,或者在您尝试时它已过期。令牌有大约一小时的到期窗口。

编辑您的 OAuth 令牌错误问题的答案:更改您的/oauth/authorizeURL 以反映您尝试进行身份验证的域,而不是soda.demo.socrata.com. 令牌与发行它们的域相关联。我将更新文档以使其更清楚。

于 2017-02-10T16:24:12.773 回答