Wordpress and OAuth












1














I am quite new to wordpress and OAuth. I have two sites. One hosted on the web,and the second one hosted on my local machine. I want to use OAuth in order to login to my local machine site if i am logged in to the other site.



What i managed to do:




  1. I had installed WP OAuth Server on web hosted site and created new client;


  2. On my local machine i wrote the following code using https://github.com/adoy/PHP-OAuth2;






    require('vendor/autoload.php');

    const CLIENT_ID = 'my client id';
    const CLIENT_SECRET = 'my client secret';

    const REDIRECT_URI = '';
    const AUTHORIZATION_ENDPOINT = '';
    const TOKEN_ENDPOINT = '';

    $client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET);
    if (!isset($_GET['code']))
    {
    $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
    header('Location: ' . $auth_url);
    die('Redirect');
    }
    else
    {
    $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
    $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
    $response = (object) $response;
    $result = (object) $response->result;
    //echo json_encode($result->access_token);
    echo "<pre>";
    print_r($response);
    }



When i run this script i got the following response:



stdClass Object
(
[result] => Array
(
[access_token] => rhfnpz6ifgnjufoq0rby0utphtzpxhrmsytnjh5z
[expires_in] => 86400
[token_type] => Bearer
[scope] => basic
[refresh_token] => lgrklt5kwiv29ch3nmdhhieedjbudg8liv48l6c7
)

[code] => 200
[content_type] => application/json
)


How can i use this access token in order to login to my local hosted site? What i am missing? Thank you!










share|improve this question



























    1














    I am quite new to wordpress and OAuth. I have two sites. One hosted on the web,and the second one hosted on my local machine. I want to use OAuth in order to login to my local machine site if i am logged in to the other site.



    What i managed to do:




    1. I had installed WP OAuth Server on web hosted site and created new client;


    2. On my local machine i wrote the following code using https://github.com/adoy/PHP-OAuth2;






      require('vendor/autoload.php');

      const CLIENT_ID = 'my client id';
      const CLIENT_SECRET = 'my client secret';

      const REDIRECT_URI = '';
      const AUTHORIZATION_ENDPOINT = '';
      const TOKEN_ENDPOINT = '';

      $client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET);
      if (!isset($_GET['code']))
      {
      $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
      header('Location: ' . $auth_url);
      die('Redirect');
      }
      else
      {
      $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
      $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
      $response = (object) $response;
      $result = (object) $response->result;
      //echo json_encode($result->access_token);
      echo "<pre>";
      print_r($response);
      }



    When i run this script i got the following response:



    stdClass Object
    (
    [result] => Array
    (
    [access_token] => rhfnpz6ifgnjufoq0rby0utphtzpxhrmsytnjh5z
    [expires_in] => 86400
    [token_type] => Bearer
    [scope] => basic
    [refresh_token] => lgrklt5kwiv29ch3nmdhhieedjbudg8liv48l6c7
    )

    [code] => 200
    [content_type] => application/json
    )


    How can i use this access token in order to login to my local hosted site? What i am missing? Thank you!










    share|improve this question

























      1












      1








      1







      I am quite new to wordpress and OAuth. I have two sites. One hosted on the web,and the second one hosted on my local machine. I want to use OAuth in order to login to my local machine site if i am logged in to the other site.



      What i managed to do:




      1. I had installed WP OAuth Server on web hosted site and created new client;


      2. On my local machine i wrote the following code using https://github.com/adoy/PHP-OAuth2;






        require('vendor/autoload.php');

        const CLIENT_ID = 'my client id';
        const CLIENT_SECRET = 'my client secret';

        const REDIRECT_URI = '';
        const AUTHORIZATION_ENDPOINT = '';
        const TOKEN_ENDPOINT = '';

        $client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET);
        if (!isset($_GET['code']))
        {
        $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
        header('Location: ' . $auth_url);
        die('Redirect');
        }
        else
        {
        $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
        $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
        $response = (object) $response;
        $result = (object) $response->result;
        //echo json_encode($result->access_token);
        echo "<pre>";
        print_r($response);
        }



      When i run this script i got the following response:



      stdClass Object
      (
      [result] => Array
      (
      [access_token] => rhfnpz6ifgnjufoq0rby0utphtzpxhrmsytnjh5z
      [expires_in] => 86400
      [token_type] => Bearer
      [scope] => basic
      [refresh_token] => lgrklt5kwiv29ch3nmdhhieedjbudg8liv48l6c7
      )

      [code] => 200
      [content_type] => application/json
      )


      How can i use this access token in order to login to my local hosted site? What i am missing? Thank you!










      share|improve this question













      I am quite new to wordpress and OAuth. I have two sites. One hosted on the web,and the second one hosted on my local machine. I want to use OAuth in order to login to my local machine site if i am logged in to the other site.



      What i managed to do:




      1. I had installed WP OAuth Server on web hosted site and created new client;


      2. On my local machine i wrote the following code using https://github.com/adoy/PHP-OAuth2;






        require('vendor/autoload.php');

        const CLIENT_ID = 'my client id';
        const CLIENT_SECRET = 'my client secret';

        const REDIRECT_URI = '';
        const AUTHORIZATION_ENDPOINT = '';
        const TOKEN_ENDPOINT = '';

        $client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET);
        if (!isset($_GET['code']))
        {
        $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
        header('Location: ' . $auth_url);
        die('Redirect');
        }
        else
        {
        $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
        $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
        $response = (object) $response;
        $result = (object) $response->result;
        //echo json_encode($result->access_token);
        echo "<pre>";
        print_r($response);
        }



      When i run this script i got the following response:



      stdClass Object
      (
      [result] => Array
      (
      [access_token] => rhfnpz6ifgnjufoq0rby0utphtzpxhrmsytnjh5z
      [expires_in] => 86400
      [token_type] => Bearer
      [scope] => basic
      [refresh_token] => lgrklt5kwiv29ch3nmdhhieedjbudg8liv48l6c7
      )

      [code] => 200
      [content_type] => application/json
      )


      How can i use this access token in order to login to my local hosted site? What i am missing? Thank you!







      php wordpress oauth






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 19 '17 at 12:41









      Codrut Tapu

      11116




      11116
























          1 Answer
          1






          active

          oldest

          votes


















          0














          After you have a response with the access_token you can log the user in without having to accept and process the normal WP email+password flow. Do this by




          1. session the access_token and refresh_token (for future calls to the API Resource endpoint, if any)

          2. call wp_set_auth_cookie() to create a WP session for the user.


          You'll want to include in your code a hook for the WP logout where you'll clear the tokens from the session.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43496083%2fwordpress-and-oauth%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            After you have a response with the access_token you can log the user in without having to accept and process the normal WP email+password flow. Do this by




            1. session the access_token and refresh_token (for future calls to the API Resource endpoint, if any)

            2. call wp_set_auth_cookie() to create a WP session for the user.


            You'll want to include in your code a hook for the WP logout where you'll clear the tokens from the session.






            share|improve this answer


























              0














              After you have a response with the access_token you can log the user in without having to accept and process the normal WP email+password flow. Do this by




              1. session the access_token and refresh_token (for future calls to the API Resource endpoint, if any)

              2. call wp_set_auth_cookie() to create a WP session for the user.


              You'll want to include in your code a hook for the WP logout where you'll clear the tokens from the session.






              share|improve this answer
























                0












                0








                0






                After you have a response with the access_token you can log the user in without having to accept and process the normal WP email+password flow. Do this by




                1. session the access_token and refresh_token (for future calls to the API Resource endpoint, if any)

                2. call wp_set_auth_cookie() to create a WP session for the user.


                You'll want to include in your code a hook for the WP logout where you'll clear the tokens from the session.






                share|improve this answer












                After you have a response with the access_token you can log the user in without having to accept and process the normal WP email+password flow. Do this by




                1. session the access_token and refresh_token (for future calls to the API Resource endpoint, if any)

                2. call wp_set_auth_cookie() to create a WP session for the user.


                You'll want to include in your code a hook for the WP logout where you'll clear the tokens from the session.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 27 '18 at 21:34









                J. LaRosee

                4992622




                4992622






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43496083%2fwordpress-and-oauth%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'