Recurring Application Charge, unable to GET OR POST

Highlighted
Tourist
26 0 1

I'm new at shopify app development with PHP. But it always shows /admin/recurring_application_charges.json?X-Shopify-Access-Token=` resulted in a `401 Unauthorized` response: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

 

Can anyone help me?

0 Likes
Explorer
98 0 15

Did you get an access token for the shop via OAuth in the first place?

Looga.io
0 Likes
Tourist
26 0 1

Yes, I've got an access token for the shop via OAuth in the first place. then call the recurring charge API.

And get requests are working fine, But the post request show error for recurring charge API.

Now it gives me error like

 Client error: `POST https://baseonesolutions-jwellery-demo.myshopify.com/admin/recurring_application_charges.json` resulted in a `401 Unauthorized` response: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

And I've passed the following array as parameter,

'recurring_application_charge'  => array(
                                    'X-Shopify-Access-Token'  =>  'my ACCESS TOKEN',
                                    'Content-Type' => 'application/json',
                                    'charset' => 'utf-8',
                                    "name" => "Super Duper Plan",
                                    "price" => 10.0,
                                    "return_url" => "https://baseonesolutions-jwellery-demo.myshopify.com/",
                                    "test" => true,
                                ),

But It produces error.

0 Likes
Explorer
98 0 15

I'm not 100% sure how you do it in the end, because the content-type in your example is "application/json", but the actual data is an PHP array.

I used this library for PHP: https://github.com/phpclassic/php-shopify

Looking at the code shows that the accesstoken must be a header field of the post call.

All the other field which belong to the charge directly, have to be in the body of the call.

You could try to follow the code of the library.

Looga.io
1 Like
Tourist
26 0 1

Thanks for your help. It already works. I've just need to put the access token before decalring "recurring_charge" array. Actually it was my mistake.

Now the code looks like

$response = $client->request(
                        'POST', 
                        "https://{$shop_domain}/admin/recurring_application_charges.json",
                        [
                            'form_params' => [
                                'access_token'  =>  $access_token_no,
                                'Content-Type' => 'application/json',
                                'Accept' => 'application/json',
                                'charset' => 'utf-8',
                                'recurring_application_charge'  => array(
                                    "name" => "Super Duper Plan",
                                    "price" => 10.0,
                                    "return_url" => "https://baseonesolutions-jwellery-demo.myshopify.com/",
                                    "test" => true,
                                ),
                            ]
                        ]
                    );

and it is working

0 Likes