title: Authentication failed due to invalid authentication credentials or a missing Authorization header. in nodejs paypal sdk
body: <p>I am trying to implement paypal subscription api in react-nodejs project.i refer <a href="https://developer.paypal.com/docs/api/subscriptions/v1/" rel="nofollow noreferrer">https://developer.paypal.com/docs/api/subscriptions/v1/</a>. after that i got id wit &quot;P-*********&quot;.
  What i have tried is:</p>
  <ol>
  <li>in Ui Side(React) i created an event for requesting server side to create the billing plans.</li>
  <li>in server side(nodejs) i do billingPlan create and update actions.</li>
  </ol>
  <p>The code is:(nodejs)</p>
  <pre><code>export const paypalSubscribe = async (user, data) =&gt; {
    const customerId = user.customer,
  { invoice: invoiceId } = data;
  
  try {
      const billingPlanAttributes = {
    description: &quot;Create Plan for Regular&quot;,
    merchant_preferences: {
      auto_bill_amount: &quot;yes&quot;,
      cancel_url: &quot;http://www.cancel.com&quot;,
      initial_fail_amount_action: &quot;continue&quot;,
      max_fail_attempts: &quot;1&quot;,
      return_url: &quot;http://www.success.com&quot;,
      setup_fee: {
        currency: &quot;USD&quot;,
        value: &quot;25&quot;
      }
    },
    name: &quot;Testing1-Regular1&quot;,
    payment_definitions: [
      {
        amount: {
          currency: &quot;USD&quot;,
          value: order.price.recurringAmount
        },
        charge_models: [
          {
            amount: {
              currency: &quot;USD&quot;,
              value: &quot;10.60&quot;
            },
            type: &quot;SHIPPING&quot;
          },
          {
            amount: {
              currency: &quot;USD&quot;,
              value: &quot;20&quot;
            },
            type: &quot;TAX&quot;
          }
        ],
        cycles: &quot;0&quot;,
        frequency: &quot;MONTH&quot;,
        frequency_interval: order.billingCycle,
        name: &quot;Regular 1&quot;,
        type: &quot;REGULAR&quot;
      }
    ],
    type: &quot;INFINITE&quot;
  };
  
  const createdBillingPlan = await new Promise((resolve, reject) =&gt; {
    Paypal.billingPlan.create(billingPlanAttributes, function (
      error,
      billingPlan
    ) {
      if (error) {
        reject(error);
      } else {
        resolve(billingPlan);
      }
    });
  });
  
  console.log(&quot;data123....&quot;, createdBillingPlan);
  
  // update
  var billing_plan_update_attributes = [
    {
      op: &quot;replace&quot;,
      path: &quot;/&quot;,
      value: {
        state: &quot;ACTIVE&quot;
      }
    }
  ];
  
  console.log(
    &quot;billing_plan_update_attributes&quot;,
    billing_plan_update_attributes
  );
  
  const updateBillingPlan = await new Promise((resolve, reject) =&gt; {
    Paypal.billingPlan.update(
      createdBillingPlan.id,
      billing_plan_update_attributes,
      function (error, response) {
        if (error) {
          reject(error);
        } else {
          resolve(response);
        }
      }
    );
  });
  
  const getBillingPlan = await new Promise((resolve, reject) =&gt; {
    Paypal.billingPlan.get(createdBillingPlan.id, function (
      error,
      updatedBillingPlan
    ) {
      if (error) {
        console.log(&quot;errr&quot;, error.response);
        reject(error);
      } else {
        console.log(&quot;updatedBillingPlan&quot;, JSON.stringify(updatedBillingPlan));
        resolve(updatedBillingPlan);
        updatedBillingPlan.redire
      }
    });
  });
  console.log(&quot;getBillingPlan&quot;, getBillingPlan);
  return { ok: true, data: getBillingPlan };
  } catch (error) {
  console.log(&quot;error&quot;, error);
  }
  };
  </code></pre>
  <p>And i got getBillingPlan is like this:</p>
  <pre><code>  { id: 'P-**************',
      state: 'ACTIVE',
      name: 'Testing1-Regular1',
      description: 'Create Plan for Regular',
      type: 'INFINITE',
      payment_definitions:
      [ { id: 'PD-0EF41434TA3045459BCMIRMA',
     name: 'Regular 1',
     type: 'REGULAR',
     frequency: 'Month',
     amount: [Object],
     cycles: '0',
     charge_models: [Array],
     frequency_interval: '1' } ],
      merchant_preferences:
     { setup_fee: { currency: 'USD', value: '25' },
   max_fail_attempts: '1',
   return_url: 'http://www.success.com',
   cancel_url: 'http://www.cancel.com',
   auto_bill_amount: 'YES',
   initial_fail_amount_action: 'CONTINUE' },
   create_time: '2020-07-01T04:18:01.008Z',
   update_time: '2020-07-01T04:18:02.031Z',
   links:
        [ { href:
      'https://api.sandbox.paypal.com/v1/payments/billing-plans/P-***********',
     rel: 'self',
     method: 'GET' } ],
     httpStatusCode: 200 
     }
  </code></pre>
  <p>And when i trying to open the links in links array
  ie,https://api.sandbox.paypal.com/v1/payments/billing-plans/P-***********' i got the error:</p>
  <pre><code>&quot;Authentication failed due to invalid authentication credentials or a missing Authorization 
  header.&quot;
  </code></pre>
  <p>Where i went wrong? How can i resolve this and implement subscription of paypal in my Project.</p>
  <p><strong>REST Api</strong></p>
  <p>i changed my code to rest api calls finaly i got response like this:</p>
  <pre><code>{ status: 'APPROVAL_PENDING',
    id: 'I-1FU83BNMBCFS',
    create_time: '2020-07-06T09:47:02Z',
    links:
         [ { href:
      'https://www.sandbox.paypal.com/webapps/billing/subscriptions? ba_token=BA-3D945638N1691194P',
     rel: 'approve',
     method: 'GET' },
   { href:
      'https://api.sandbox.paypal.com/v1/billing/subscriptions/I- 1FU83BNMBCFS',
     rel: 'edit',
     method: 'PATCH' },
   { href:
      'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-1FU83BNMBCFS',
     rel: 'self',
     method: 'GET' } ],
     responseCode: 201 }
  </code></pre>
  <p>and in my ui side i opened the approval link in new window after submitting it shows 404. Why?</p>
  <p>code:</p>
  <pre><code> window.open(URL, &quot;_blank&quot;);
  </code></pre>
  <p>UPDATE:Subscription:</p>
  <pre><code> const subscriptionString = {
      plan_id: result.id,
      start_time: &quot;2021-11-01T00:00:00Z&quot;,
      shipping_amount: {
        currency_code: &quot;USD&quot;,
        value: &quot;10.00&quot;
      },
      subscriber: {
        name: {
          given_name: &quot;John&quot;,
          surname: &quot;Doe&quot;
        },
        email_address: &quot;customer@example.com&quot;,
        shipping_address: {
          name: {
            full_name: &quot;John Doe&quot;
          },
          address: {
            address_line_1: &quot;2211 N First Street&quot;,
            address_line_2: &quot;Building 17&quot;,
            admin_area_2: &quot;San Jose&quot;,
            admin_area_1: &quot;CA&quot;,
            postal_code: &quot;95131&quot;,
            country_code: &quot;US&quot;
          }
        }
      },
      application_context: {
        brand_name: &quot;walmart&quot;,
        locale: &quot;en-US&quot;,
        shipping_preference: &quot;SET_PROVIDED_ADDRESS&quot;,
        user_action: &quot;SUBSCRIBE_NOW&quot;,
        payment_method: {
          payer_selected: &quot;PAYPAL&quot;,
          payee_preferred: &quot;IMMEDIATE_PAYMENT_REQUIRED&quot;
        },
        return_url: &quot;https://example.com/returnUrl&quot;,
        cancel_url: &quot;https://example.com/cancelUrl&quot;
      }
    },
    options = {
      url: &quot;https://api.sandbox.paypal.com/v1/billing/subscriptions&quot;,
      method: &quot;POST&quot;,
      headers: headers,
      body: JSON.stringify(subscriptionString)
    },
    activateResult = await payment.callPayaplApi(options);
  
  
    return {
        ok: true,
        data: activateResult
    };
    }
  </code></pre>