2017-11-29 6 views
1

enter image description here Ich entwickle eine App für Shopify. Derzeit in der Entwicklungsphase. Bis jetzt ist es mir gelungen, die App zu autorisieren und sie dann mit dem Embedded App SDK zurück auf die Admin-Seite umzuleiten. Wenn ich jedoch auf die Admin-Seite zurückkomme, gibt es eine Fehlermeldung, die besagt: Request origin cannot be verified.Anfrage Herkunft kann nicht verifiziert werden - Shopify

Die Konsole zeigt Failed to load resource: the server responded with a status of 403 (Forbidden) Die URL in der Konsole ist so etwas wie dieses https://myshop.myshopify.com/admin/apps/dfdjf4343343434343434bfdf/shopify/shopify/callback?code=ffdfdffd&hmac=fdfdfdfdfdfdfdfdfddfdfdfdfdf&shop=myshop.myshopify.com&state=151193864548800&timestamp=1511938648

Die fdfdfdfdfdfdfdfdfddfdfdfdfdf sind nur zufällige Zeichen, die ich anstelle eines Hash ersetzt haben. FYI - Ich habe den Namen und den Namen des Benutzerprofils und des Avatars aus dem Bild entfernt.

Antwort

1

Dies liegt daran, geschieht, Sie nicht in der Lage sind Zustand übereinstimmen, wird das in Cookie gesetzt, während mit Redirect-URL

const ShopifyToken = require('shopify-token') 
 

 
const forwardingAddress = process.env.HOST 
 

 
const shopifyToken = new ShopifyToken({ 
 
    sharedSecret: process.env.SHOPIFY_API_SECRET, 
 
    redirectUri: forwardingAddress + '/shopify/callback', 
 
    apiKey: process.env.SHOPIFY_API_KEY 
 
}) 
 

 

 
const shopify = { 
 
    // use this for authentication 
 
    auth: (req, res, next) => { 
 
    const shop = req.query.shop 
 
    if (!shop) { 
 
     return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request') 
 
    } 
 
    const shopRegex = /^([\w-]+)\.myshopify\.com/i 
 
    const shopName = shopRegex.exec(shop)[1] 
 
    const state = shopifyToken.generateNonce() 
 
    const url = shopifyToken.generateAuthUrl(shopName, scopes, state) 
 
    res.cookie('state', state) 
 
    res.redirect(url) 
 
    }, 
 

 
    // use this as your callback function 
 
    authCallback: async (req, res) => { 
 
    const { shop, hmac, code, state } = req.query 
 
    const stateCookie = cookie.parse(req.headers.cookie).state 
 
    if (state !== stateCookie) { 
 
    // you are unable to set proper state ("nonce") in this case, thus you are getting this error 
 
     return res.status(403).send('Request origin cannot be verified') 
 
    } 
 
    if (!shop || !hmac || !code) { 
 
     res.status(400).send('Required parameters missing') 
 
    } 
 
    let hmacVerified = shopifyToken.verifyHmac(req.query) 
 
    console.log(`verifying -> ${hmacVerified}`) 
 
    // DONE: Validate request is from Shopify 
 
    if (!hmacVerified) { 
 
     return res.status(400).send('HMAC validation failed') 
 
    } 
 
    const accessToken = await shopifyToken.getAccessToken(shop, code) 
 
    const shopRequestUrl = 'https://' + shop + '/admin/shop.json' 
 
    const shopRequestHeaders = { 
 
     'X-Shopify-Access-Token': accessToken 
 
    } 
 
    try { 
 
     const shopResponse = await request.get(shopRequestUrl, { headers: shopRequestHeaders }) 
 
     res.status(200).end(shopResponse) 
 
    } catch (error) { 
 
     res.status(error.statusCode).send(error.error.error_description) 
 
    } 
 
    } 
 
}

reagiert
Verwandte Themen