I have some python code which purpose is to, using the BoxSDK, download all files in a folder, generate some new Summary files and upload those files to a different output folder.
I’m accomplishing this with the methods:
client.folder(folder_id=input_folder_id).get_items()
client.folder(output_folder_id).upload_stream(t_buffer, f"{cliente.name}_Demo.csv")
on the Developer console I generated a Custom App, with “Server Authentication (with JWT)” as the Auth method, and after adding it to the admin console have been able to work with it perfectly fine using the developer tokens.
After testing and developing for a bit I finally got the code to where it needs to be, now its time to move away from the 1 hour developer tokens and get into a more permanent authorization.
But this is where my problems began as I have not been able to get this to work.
After reading the documentation on how to select an Auth method OAuth 2.0 seemed like the correct choice, as my box application will only have one user (Myself), and said user account is where I want to get and upload all my files from, this account is already set up with all my files so this seemed like the best choice.
After reading the Setup with OAuth 2.0 page i tried to get it working but I have not been able to do so.
I Created a new Custom App, this time with the “User Authentication (OAuth 2.0)” Auth method, added in my Redirect URI as http://localhost:5000/oauth2callback, gave the proper permissions under Application Scopes, and added it to the admin console, noting that this time it was added under “User Authentication Apps”.
Then I made a small Flask application:
from flask import Flask, request
import requests
URL="https://account.box.com/api/oauth2/authorize"
URL2 = "https://api.box.com/oauth2/token"
PARAMS = {
"client_id" : "_",
"redirect_uri": "http://localhost:5000/oauth2callback",
"response_type":"code"
}
data = {
"client_id" : "_",
"client_secret": "_",
"grant_type":"client_credentials"
}
app = Flask(__name__)
@app.route("/")
def getAuth():
r = requests.get(URL, params=PARAMS)
return r.text
@app.route("/oauth2callback")
def callback():
data["code"] = request.args.get("code")
r = requests.post(URL2, data=data)
print(data["code"])
return r.text
if __name__ == '__main__':
app.run(debug = True)
Just with the purpose of huosing the Box auth web portal and do the necesary steps to get my token.
I ran it , and everything seemed to work, the portal poped up, I logged in into my box account and clicked the button that said “Grant access to box”, and got a response with my AuthToken
but the token response I got seemed a bit weird.
"GET /oauth2callback?code=_ HTTP/1.1"
As it was missing a Refresh token, but I paid it no mind and just thought that I would deal with that latter.
But when I tried ruining the same code that was working with the developer tokens I could not get it to work, it kept telling me that It was not able to find the folder with the id I provided:
Context Info: {'errors': [{'reason': 'invalid_parameter', 'name': 'folder', 'message': "Invalid value 'd_'. 'folder' with value 'd_' not found"}]}
Its not and Auth error, but it seems like the hole OAuth 2.0 thing did not work.
I then thought maybe I was doing something wrong and decided to try using postman, as it has a easy to use OAuth 2.0 portal, but when I tried that, the Auth Failed, the postman console telling me
“Error: The client credentials are invalid”
What am I doing Wrong?
