2017-11-29 10 views
1

enter image description here Shopify 용 앱을 개발 중입니다. 현재 개발 단계입니다. 지금까지 앱을 승인 한 다음 임베디드 앱 SDK를 사용하여 관리자 페이지로 다시 리디렉션했습니다. 그러나 관리자 페이지로 돌아 가면 Request origin cannot be verified이라는 오류 메시지가 나타납니다.요청 원을 확인할 수 없습니다. - Shopify

는 콘솔 Failed to load resource: the server responded with a status of 403 (Forbidden) 을 보여줍니다 콘솔의 URL은 fdfdfdfdfdfdfdfdfddfdfdfdfdf 내가 대신 해시의 대체 한 무작위 문자입니다이 https://myshop.myshopify.com/admin/apps/dfdjf4343343434343434bfdf/shopify/shopify/callback?code=ffdfdffd&hmac=fdfdfdfdfdfdfdfdfddfdfdfdfdf&shop=myshop.myshopify.com&state=151193864548800&timestamp=1511938648

같은 것입니다. 참고 - 이미지에서 앱 이름과 사용자 프로필 이름 및 아바타를 삭제했습니다. 이 때문에 일어나고

답변

1

, 당신은 상태와 일치 할 수 없습니다 리디렉션 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) 
 
    } 
 
    } 
 
}