How to use OAuth to place an order

Highlighted
Shopify Partner
28 0 1

I want to place an order via the API. But would rather use OAuth2.

How to convert the code below to OAuth style? The code is based on:

 

Example with Username + password   - Shows how to use Username + password to post add a new product.

https://help.shopify.com/api/tutorials/using-postman

 

Example with OAuth2 - only shows web hooks - no details of how to place an order

https://help.shopify.com/api/tutorials/building-node-app

 

Code - with Username + password

const dotenv = require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const crypto = require('crypto');
const cookie = require('cookie');
const nonce = require('nonce')();
const querystring = require('querystring');
const request = require('request-promise');


....



   const placeOrderRequestUrl = 'https://' + shop + '/admin/orders.json';
   const placeOrderPayload = {
     "order": {
       "line_items": [
       { "quantity": 1,
         "variant_id": 606602985481,
         "title": "EMILY SINGLET DRESS ",
         "price": "7.75",
         "product_id": 60511617033,
         "gift_card": false }
       ]
     },
   };

   request.post(placeOrderRequestUrl , {  json: placeOrderPayload })
   .auth(apiUser , apiPassword, false)
   .then(function (response) {
        console.log("placeOrder succeeded with status %d", response.statusCode);
     })
     .catch((error) => {
       console.log("placeOrder failed");       
       console.log(error);
     });

How to convert this to OAuth style?

 

0 Likes
Shopify Staff
Shopify Staff
1137 24 162

Hey Eddy.

Not sure I understand what you mean when you say "oauth style". Oauth is used to acquire an online or offline access token. Once you have an access token, you include it in the header of your request as a value for `X-Shopify-Access-Token`.

After writing that I assume you are looking for an alternative to Basic authentication. In which case, the above applies. First get an access token, then make requests specifying X-Shopify-Access-Token in the header.

That's shown in the Node tutorial here:

      const shopRequestUrl = 'https://' + shop + '/admin/shop.json';
      const shopRequestHeaders = {
        'X-Shopify-Access-Token': accessToken,
      };

      request.get(shopRequestUrl, { headers: shopRequestHeaders })
      .then((shopResponse) => {
        res.status(200).end(shopResponse);
      })

 

1 Like
Shopify Partner
28 0 1

Thanks. Yes that is the answer. I missed it the first time through - all working now.

 

0 Likes