I have a script on a VPS that uploads files to my youtube channel. This is a headless server which I use via a ssh session. I am having a problem with the authorisation part. When I run my script, I am asked to authorise myself using the link provided. As I cannot use a browser on my VPS, I can only paste this link on a browser on my local computer. When I do this, I am able to log in and approve the application but I get redirected to a localhost url - which in my case is not working as I am not opening the url from my VPS. Google's documentation suggests to just use a localhost redirect uri in the authorisation flow which I am but I am now unable to actually authorise the application from a local computer as I cannot do this via the server where the script is running.
Here is the part of my script that deals with authentication:
def authyt():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
try:
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
creds.refresh(Request())
except google.auth.exceptions.RefreshError as error:
# if refresh token fails, reset creds to none.
creds = None
print(f'Refresh token expired requesting authorization again: {error}')
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
creds = flow.run_local_server(port=2837)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
return build(API_SERVICE_NAME, API_VERSION, credentials = creds)
Here is my client_secrets.json file
{
"installed": {
"client_id": "CLIENT ID",
"project_id": "PROJECT ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "CLIENT SECRET",
"redirect_uris": ["http://localhost:2837/"]
}
}
I have already entered the http://localhost:2837/ in my google console so there is no uri mismatch error.
The issue you are having is that the YouTube api only supports a single form for authorization. with the exception of content owners that own and manage multiple YouTube channels (Note I am and have never gotten this to work. I suspect you may need to be white listed.)
The code you are using will connect to a standard YouTube account using your google account.
In the past I have used this method to help serval clients do exactly what you are doing.
When you run the code the access token and refresh token are stored in the 'token.json' file.
If you open the file it should look something like this
{
"token": "[redacted]",
"refresh_token": "[redacted]",
"token_uri": "https://oauth2.googleapis.com/token",
"client_id": "[redacted]",
"client_secret": "[redacted]",
"scopes": [
"https://www.googleapis.com/auth/youtube"
],
"expiry": "2022-09-05T07:22:56.175239Z"
}
In that file contains everything that the application will need the next time it runs to request a new access token.
If you want this code to run on your server. What you need to do is run it once on your machine locally, ensure that a token.json file has been created. then upload the whole thing to your server. it will then run automated without any further authorization required from you.
NOTE: If your application is still in the testing phase over on google cloud console then your refresh token is going to expire after seven days. You simply need to click the button and set your application over to production. Once that is done refresh tokens will no longer expire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With