I will introduce a sample that uses the API of the file sharing cloud service "BOX" from Python.
Various people have introduced how to use the API, including the "BOX" document. I didn't have any information close to what I wanted to do, so I wrote an article.
https://github.com/box/box-python-sdk/blob/master/docs/usage/authentication.md First of all, I wanted to run the following sample code on this SDK site quickly.
client = Client(auth)
user = client.user().get()
print('User ID is {0}'.format(user.id))
If you just move it, you can manually pass OAuth authentication, but this time I tried to realize OAuth by a simple method.
Mac 10.14.6
Python 3.7.4
boxsdk 2.6.1
Please install with pip or other tools according to your environment.
pip install boxsdk
https://developer.box.com
Copy the client ID and client sensitive code displayed here to the source.
Enter "http: // localhost: 8080" for the redirect URI.
sample.py
from boxsdk import OAuth2, Client
import webbrowser
import http.server
import socketserver
from urllib.parse import urlparse, parse_qs
#Get from the BOX management screen
CLIENT_ID = 'Client ID'
CLIENT_SECRET = 'Client sensitive code'
#Set on the BOX management screen
REDIRECT_URI = 'http://localhost:8080'
HOST = '127.0.0.1'
PORT = 8080
#Variable to put the authentication code issued by BOX
global auth_code
auth_code = None
oauth = OAuth2(
client_id = CLIENT_ID,
client_secret = CLIENT_SECRET,
store_tokens = None #Token storage is omitted this time
)
#Start OAuth
auth_url, csrf_token = oauth.get_authorization_url(REDIRECT_URI)
#Start your browser and enter your BOX ID and password
#REDIRECT when entered_Redirected to URI
webbrowser.open(auth_url)
# REDIRECT_Processing when URI is hit
class ServerHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
global auth_code
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"<h1>Authenticated</h1>")
parsed_path = urlparse(self.path)
query = parse_qs(parsed_path.query)
auth_code = query['code'][0]
with socketserver.TCPServer((HOST, PORT), ServerHandler) as server:
print('http server start')
# server.serve_forever() # Ctrl+Continue processing until there is an interrupt such as C being pressed
server.handle_request() #Exit after processing one request
print('http server shutdown')
# auth_Now that you have the code, you can use the API from here
access_token, refresh_token = oauth.authenticate(auth_code)
client = Client(oauth)
me = client.user().get()
print('My user ID is {0}'.format(me.id))
I wanted to run it in JupyterLab, so I wrote it in one file.
It's easy to start a simple web server in Python. This time, I like the fact that the web server is terminated after processing one request.
On the contrary, I don't like the fact that the do_GET method assigns directly to a global variable. Is there a beautiful way to write it?
I don't think it's working strangely, but I'm not familiar with Python yet, so I'd appreciate it if you could point out any strange points or improvements.
Recommended Posts