d
Topic
Posts:
26
12 months ago

Recurring Application Charge, unable to GET OR POST This post is outdated

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?

i
Replies
Felix Member looga.io
Posts:
98
12 months ago

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

Looga.io
Posts:
26
12 months ago

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.

Felix Member looga.io
Posts:
98
Last edited 12 months ago
g
1
upvotes

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
Posts:
26
12 months ago

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