Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:

  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

oauth what is it?

billy2billy2 Member, OS Professional, Mentor, Developers Posts: 1,190 PRO
edited January 2018 in App Store & Partners Apps


oauth video



I can do this in 5 lines of code. I'll post the code later.

Comments

  • billy2billy2 Member, OS Professional, Mentor, Developers Posts: 1,190 PRO
    edited January 2018
    An Onshape app requires something called oauth to allow your website to talk to their servers. Here's the oauth code used on my new site. To see this work, cut the url address below and paste it into your browser.

    1. It starts with the web address and in my case its:
    https://rndengineering.com/pro/


    2. There's no parameters sent in this address so I capture this and send the user to onshape. Here's the code that checks for parameters and re-locates to onshape:
     
    //handle oauth code, no code?, go get it
    if (!$_REQUEST["code"])  
    echo '<script>location = "https://oauth.onshape.com/oauth/authorize?response_type=code&client_id=DZ3JRO3ECFWZY7ZVY%3D";</script>';


    3. This sends the user off to Onshape so they can allow the user to sign in. Upon success, they'll send the user back to my site base on a url return address I registered in the app store. Here's the updated address Onshape sends back:

    https://rndengineering.com/pro/?code=vfmAcpHiNkFRkl

    The url address above won't work if you copy it into a browser, it's a deadend and goes nowhere, timed out. Notice that Onshape adds a code to the url.


    4. With a code returned from Onshape, I request a token from Onshape:
    //have code so process
    $token="curl -X POST -H \"Content-Type:application/x-www-form-urlencoded\" -d \"grant_type=authorization_code&code=$_REQUEST["code"]&client_id=DZ3JROH7SNRVW2Y7ZVY%3D&client_secret=C2A2VUGAHV4H2XM5SNEC5VQ%3D%3D%3D%3D\"  \"https://oauth.onshape.com/oauth/token\" ";

    The ultimate piece of information, the reason for all this interaction is inside $token.

    5. Grab the 'access_token'. It's needed for all subsequent calls to Onshape servers and it only lasts for 60 minutes. Get the access token:

    //yeah just what I need
    $token['access_token']


    Here's a video link to my latest code:
    R&D pro release candidate


  • bruce_williamsbruce_williams Member, Developers Posts: 309 PRO
    @billy2

    Thanks for the app! Looks really useful.  Lack of display states & better property management has been a big deal for adoption of OS by my engineers.  Will look forward to your next releases and in meantime I am going to start testing it out.  
  • billy2billy2 Member, OS Professional, Mentor, Developers Posts: 1,190 PRO
    Thanks Bruce, let me know what you find.


Sign In or Register to comment.