【问题标题】:How to pass the Zoho-oauthtoken in the Header using Guzzle on Laravel?如何在 Laravel 上使用 Guzzle 在 Header 中传递 Zoho-oauthtoken?
【发布时间】:2020-05-25 04:47:55
【问题描述】:

我想为 Guzzle 转换这个 cURL

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>"{\n    \"plan\": {\n        \"plan_code\": \"AM-001\",\n        \"price\": " . $gPrice . ",\n        \"tax_id\": \"1786305000000842230\",\n    },\n    \"addons\": [\n        {\n            \"addon_code\": \"AB-001\",\n            \"addon_description\": \"Ads Budget\",\n            \"price\": " . $bPrice . ",\n\n        }\n    ],\n    \"coupon_code\": \"150-credit\"\n    \n}",
  CURLOPT_HTTPHEADER => array(
    "X-com-zoho-subscriptions-organizationid: " . $org_id,
    "Authorization: Zoho-oauthtoken " . $accessToken,
    "Content-Type: application/x-www-form-urlencoded"
  ),
));


现在,我已经转换了这个:


$headers = [
            'Authorization' => 'Zoho-oauthtoken ' . $access_token,
            'X-com-zoho-subscriptions-organizationid' => $org_id,
        ];

        $res = $client->request('POST', 'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription', $headers, [
            'plan' => [
                'plan_code' => 'AM-001',
                'price' => $data['finaltotal'],
                'tax_id' => '1786305000000842230',
            ],
            'addons' =>[
                'addon_code' => 'AB-001',
                'addon_description' => 'Ads Budget',
                'price' => $data['finalads']
            ],
            'coupon_code' => '150-credit'
        ]);


但我有

“客户端错误:POST https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription 导致401 Unauthorized 响应: {"code":14,"message":"为 authtoken 传递的值无效。"}"

我是否正确定义了标题?

感谢您的帮助。

【问题讨论】:

    标签: laravel http-headers guzzle octobercms zoho


    【解决方案1】:

    第三个选项是options请求,所以如果你需要通过headers你需要指定keyheaders

    所以你的代码应该是这样的

    $options = [
        'headers' => [ // <- here :)
                'Authorization' => 'Zoho-oauthtoken ' . $access_token,
                'X-com-zoho-subscriptions-organizationid' => $org_id,
        ];
    ]
    
    $res = $client->request(
        'POST',
        'https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription', 
        $options, // <- options
        [
                'plan' => [
                        'plan_code' => 'AM-001',
                        'price' => $data['finaltotal'],
                        'tax_id' => '1786305000000842230',
                ],
                'addons' =>[
                        'addon_code' => 'AB-001',
                        'addon_description' => 'Ads Budget',
                        'price' => $data['finalads']
                ],
                'coupon_code' => '150-credit'
        ]
    );
    

    试试这个应该可以的。

    如有任何疑问,请发表评论。

    【讨论】:

    • 它适用于 oauthtoken,现在我有另一个错误代码 ``“客户端错误:POST https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription 导致 400 Bad Request 响应:{“code”:40002,“message”:”此请求的请求有效负载不能为空。”}`` 我想我要为此打开一个新问题,或者您知道问题出在哪里吗?我认为是与插件有关,因为在文档zoho.com/subscriptions/api/v1/…
    • hmm 你设置了这个标题吗? :) Content-Type: application/json;charset=UTF-8
    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 2022-11-12
    • 2021-01-06
    • 2022-10-21
    • 2018-08-05
    • 2023-03-14
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多