I want to create a python script that will allow me to upload files to OneNote via command line. I have it working perfectly and it authenticates fine. However, everytime it goes to authenticate, it has to open a browser window. (This is because authentication tokens only last an hour with OneNote, and it has to use a refresh token to get a new one.) While I don't have to interact with the browser window at all, the fact that it needs to open one is problematic because the program has to run exclusively in a terminal environment. (E.g. the OneNote authentication code tries to open a browser, but it can't because there isn't a browser to open).
How can I get around this problem? Please assume it's not possible to change the environment setup.
UPDATE:
You have to get a code in order to generate an access token. This is the part that launches the browser. It is only required the first time though, for that initial token. Afterwards, refresh token requests don't need the code. (I was calling it for both, which was the issue).
That solves the problem of the browser opening each time I run my program. However, it still leaves the problem of the browser having to open that initial time. I can't do that in a terminal environment. Is there a way around that? 
E.g. Can I save the code and call it later to get the access token (how long until it expires)? Will the code work for any user, or will it only work for me?
If this is always with the same account - you can make the "browser opening and password typing" a one time setup process. Once you've authenticated, you have the "access token" and the "refresh token". You can keep using the access token for ~1hr. Once it expires, you can use the "refresh token" to exchange it for an "access token" without any user interaction. You should always keep the refresh token so you can get new access tokens later.
This is how "background" apps like "IFTTT" keep access to your account for a longer period of time.
Answer to your updated question:
The initial setup has to be through UI in a browser. If you want to automate this, you'll have to write some UI automation.
How Do I Authenticate OneNote Without Opening Browser?