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.
from YouTube Data API - How to authorise app from remote web server
No comments:
Post a Comment