【问题标题】:Deepcrawl authencation curl response null?Deepcrawl身份验证卷曲响应为空?
【发布时间】:2019-03-08 23:05:18
【问题描述】:

要对 API 调用进行身份验证,您必须首先生成会话以获取令牌。然后您必须发送会话令牌作为 X-Auth-Token 标头的值来验证 API 调用。

要创建会话,您首先需要手动生成 API 密钥。这是可以在 API 访问页面中生成的键/值对。当您单击 Generate New API Key 时,将显示一个带有 API Key Value 的弹出窗口,在 Active Keys 表中您可以找到 API Key ID。然后通过基本身份验证在 POST 调用中将其发送到会话路由。

我已经关注了 deepcrawl 的这个 Api 文档,但是我做错了什么?

我有X-Auth-TokenAPI KEY ID 我应该把这个放在哪里?

$deephead = "X-Auth-Token: asdjasiojqoieuqiouwqpofoqwojeq"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.deepcrawl.com/sessions");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $deephead);

// Get the response
$response = curl_exec($ch);

// Close cURL connection
curl_close($ch);

// Decode the response (Transform it to an Array)
$result = json_decode($response);

//debug the response
dd($result); 

我什至无法到达CURLOPT_GET,因为在我的post 我只得到空值。任何想法/想法我该如何解决这个问题。先感谢您。

【问题讨论】:

    标签: php laravel curl


    【解决方案1】:

    DeepCrawl API

    您似乎正在尝试生成一个令牌,而您似乎已经拥有该令牌。

    要对 API 调用进行身份验证,您必须首先生成会话以获取 令牌。然后您必须发送会话令牌作为 X-Auth-Token 标头用于验证 API 调用。

    要创建会话,您首先需要手动生成 API 密钥。 这是可以在 API 访问页面中生成的键/值对。 当您单击 Generate New API Key 时,将显示一个弹出窗口,其中包含 API 密钥值,在 Active Keys 表中,您可以找到 API 密钥标识。然后通过 POST 调用中的基本身份验证将其发送到 会话路由。

    curl -X POST -u '123:abcdef' https://api.deepcrawl.com/sessions { "token":"abcdef123", "_user_href":"/users/example-user", ... }

    不是最清楚的,但它就在那里。

    $headers = [
        'Content-Type: application/x-www-form-urlencoded',
        'accept: text/html'
    ];
    $username='yourusername';
    $password='supersecret';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.deepcrawl.com/sessions");
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "root=1");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    // Get the response
    $response = curl_exec($ch);
    
    // Close cURL connection
    curl_close($ch);
    
    // Decode the response (Transform it to an Array)
    return json_decode($response);
    

    这将返回类似

    {
      "token":"abcdef123",
      "_user_href":"/users/example-user",
      ...
    }
    

    由于您已经拥有令牌,因此您不需要点击会话。您可以直接拨打电话。

    $headers = [
        'X-AUTH-TOKEN: your-api-key',
        'accept: text/html'
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.deepcrawl.com/system_statuses");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    // Get the response
    $response = curl_exec($ch);
    
    // Close cURL connection
    curl_close($ch);
    
    // Decode the response (Transform it to an Array)
    return json_decode($response);
    

    【讨论】:

    • 他们真的需要用户名和密码吗?
    • 我试过这个命令,但同样的事情我只得到空值:/
    • 你打的是哪个端点?如果您的帐户中没有数据,则没有响应。
    • 我的帐户中有数据。我还有 2 个带有生成会话令牌的 api 密钥 ID。
    • 6/7 现在在我的帐户中处于活动状态。这很奇怪
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2018-09-04
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    相关资源
    最近更新 更多