Ad

How To Pass The Zoho-oauthtoken In The Header Using Guzzle On Laravel?

I have this cURL that I want to convert for Guzzle

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"
  ),
));


For now, I have convert this :


$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'
        ]);


But I have

"Client error: POST https://subscriptions.zoho.com/api/v1/hostedpages/newsubscription resulted in a 401 Unauthorized response: {"code":14,"message":"Invalid value passed for authtoken."}"

Have I correctly defined the header?

Thank you for your help.

Ad

Answer

third option is options for request, So if you need to pass headers you need to specify key headers.

so your code should look like this

$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'
    ]
);

try this it should work.

if any doubt please comment.

Ad
source: stackoverflow.com
Ad