douyingtai6662 2018-02-27 14:07
浏览 92

如何在REST API中授权发布请求?

I'm sitting here working on making a post request in a rest api in php using curl. For this purpose I have the api key and an auth key, which I am currently including as post values in the request. But I keep getting HTML back in my response instead of JSON data (which its supposed to be) giving me a 401 unauthorized error.

I've noticed often you need to make custom headers to authorize yourself in these cases (I'm guessing I need to use my auth key for that, in the header somehow).

This is my code:

$post = [
    'apikey' => $apiKey,
    'authkey' => $authKey,
    'name' => $reknr,
    'description' => $opgave,
    'clientId'   => $clientId,
    'orderTypeId' => $typeId,
    'contactAddressId' => $addressId,
    'theirref' => $ref,
    'reqno' => $reknr
];

// Set api link
$ch = curl_init('https://app.minuba.dk/api/1/Order');

// Set return value to true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Configure header
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Authorization: Basic '.$authKey,
        'Content-Type: application/json')
);

// Add post fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

The API docs doesn't say anything about how you authorize yourself when making post request, during get requests its fairly easy you just include the apikey and authkey as normal parameters and it works fine.

My question is, how do I know how to configure my header so the API authorizes me? The docs say nothing about it or offers any explanation as to how except mentioning you need both the apikey and authkey to gain access.

I admit my understand of the http headers are limited, but how do I know how to configure it for this particular api?

UPDATE:

Found out the header response gives me this line: WWW-Authenticate: Basic realm="Minuba REST API"

This tells me that the method I'm using to authenticate should be correct right? So why am I still getting a 401?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测