GithubHelp home page GithubHelp logo

pizdata / oauth2-shopify-php Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 4.0 23 KB

Shopify Provider for the OAuth 2.0 Client

License: MIT License

PHP 100.00%
shopify shopify-api shopify-php shopify-oauth

oauth2-shopify-php's Introduction

Shopify OAuth 2.0 Client Provider

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Shopify OAuth 2.0 Client Provider.

Install

Via Composer

$ composer require pizdata/oauth2-shopify

Usage

$provider = new Pizdata\OAuth2\Client\Provider\Shopify([
    'clientId'                => '{shopify-client-id}',    // The client ID assigned to you by the Shopify
    'clientSecret'            => '{shopify-client-secret}',   // The client password assigned to you by the Shopify
    'redirectUri'             => 'http://localhost/callback', // The redirect URI assigned to you
    'shop'                    => 'pizdata.myshopify.com', // The Shop name
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Setting up scope
    $options = [
        'scope' => [
            'read_content', 'write_content',
            'read_themes', 'write_themes',
            'read_products', 'write_products',
            'read_customers', 'write_customers',
            'read_orders', 'write_orders',
            'read_draft_orders', 'write_draft_orders',
            'read_script_tags', 'write_script_tags',
            'read_fulfillments', 'write_fulfillments',
            'read_shipping', 'write_shipping',
            'read_analytics',
        ]
    ];
    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl($options);

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {

    if (isset($_SESSION['oauth2state'])) {
        unset($_SESSION['oauth2state']);
    }
    
    exit('Invalid state');

} else {

    try {
        // Try to get an access token using the authorization code grant.
        $accessToken = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);

        $store = $provider->getResourceOwner($accessToken);

        // Access to Store base information
        echo $store->getName();
        echo $store->getEmail();
        echo $store->getDomain();

        // Use this to interact with an API on the users behalf
        echo $token->getToken();

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
        // Failed to get the access token or user details.
        exit($e->getMessage());

    }
}

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

oauth2-shopify-php's People

Contributors

sevastyanovio avatar tahrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.