Welcome to the new Box Support website. Check out all the details here on what’s changed.

How do I create INITIAL OAuth2 tokens?

Answered
New post

Comments

10 comments

  • jcleblanc

    Hi rollo100,

     

    This guide should help you go through the OAuth 2 process with Python: https://developer.box.com/docs/authenticate-with-oauth-2

     

    That should allow you to get the original access token. If you see under step 3 in the Python example that access_token is one of the set variables. That may be extracted if you need it.

     

    I'd recommend using the Python SDK directly, as opposed to making the direct API calls yourself, as it'll be easier to update the SDK if changes are made rather than your underlying direct API code.

     

    Let me know if you have any other questions,

    Jon

    0
    Comment actions Permalink
  • rollo100

    Thank you! I'll give it a try right away!

    0
    Comment actions Permalink
  • dkadia

    Hi,

     

    When i tried to run the Step 1 code in python, it gave me an error - 

    No module named 'config_oauth'

    I also tried to install config_oauth but it says 'could not find a version which satisfies the requirement config_oauth'.

    I am relatively new to python and box api so have very limited knowledge on where i am doing wrong. 

     

    Thanks!

    0
    Comment actions Permalink
  • jcleblanc

    Hi , you'll need to create the config file yourself, as outlined here: https://developer.box.com/docs/authenticate-with-oauth-2#section-step-1-add-config-and-dependencies

    0
    Comment actions Permalink
  • dkadia

    I am not sure if i get it. If I am passing the client ID, client Secret and Redirect URL in the code itself, still i have to create a config file?

    I am trying to get the access token and refresh token to automate the task of moving the files within my box account. 

     

    0
    Comment actions Permalink
  • jcleblanc

    No, you can either use a config file (as shown in the docs) or simply embed those three variables directly in your code and remove the line for: 

    import config_oauth

    What the Python code would look like with that removed would be something like: 

    from boxsdk import Client
    from boxsdk import OAuth2
    
    # Auth config
    client_id = 'YOUR CLIENT ID'
    client_secret='YOUR CLIENT SECRET'
    redirect_uri = 'http://127.0.0.1:5000/return'
    
    # Create new OAuth client & csrf token
    oauth = OAuth2(
      client_id=client_id,
      client_secret=client_secret
    )
    csrf_token = ''
    
    global csrf_token
    auth_url, csrf_token = oauth.get_authorization_url(redirect_uri)
    
    return redirect(auth_url)
    0
    Comment actions Permalink
  • dkadia

    Thank you! I will try the provided code. 

    0
    Comment actions Permalink
  • dkadia

    The code sometimes works well generating auth_url. But sometimes it throws an error 

    name 'auth_url' is not defined

    Any help on what i am doing wrong. 

     

    0
    Comment actions Permalink
  • Box Product Support

    I have create file called renewToken.py and write following code to it

     

     

    from boxsdk import Client
    from boxsdk import OAuth2

     

    def renewToken():

        # Auth config
        client_id = 'YOUR CLIENT ID'
        client_secret='YOUR CLIENT SECRET'
        redirect_uri = 'http://127.0.0.1:5000/return'

        # Create new OAuth client & csrf token
        oauth = OAuth2(
        client_id=client_id,
        client_secret=client_secret
        )
        csrf_token = ''

        global csrf_token
        auth_url, csrf_token = oauth.get_authorization_url(redirect_uri)

        return redirect(auth_url)

     

    My question is how do i use following code:

     

    # Fetch access token and make authenticated request
    .route('/return')
    def capture():
        # Capture auth code and csrf token via state
        code = request.args.get('code')
        state = request.args.get('state')

        # If csrf token matches, fetch tokens
        assert state == csrf_token
        access_token, refresh_token = oauth.authenticate(code)

       # PERFORM API ACTIONS WITH ACCESS TOKEN

    0
    Comment actions Permalink
  • Box Product Support

    Can you please comment your code snippet , I am new to python could find solution which will i understand.

    0
    Comment actions Permalink

Please sign in to leave a comment.