Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Google (YouTube) Auth API

$
0
0

I'm trying to use the Google Auth Python library to perform OAuth2 into my own YouTube account, so I can gather metrics of my own subscriptions. The code, however, isn't working.

I have setup a service account, and that process created a "Compatible OAuth2" client, and I exported the JSON key file from that. I have also setup an API key, which is also enabled to do all the things (yes, I know, the sec-eng soul inside you is dying) ...

Here is the code:

# Python 3.10.0from google.oauth2 import service_accountimport requestsimport jsonimport os# Start an OAuth sessionservice_account_info = json.load(open(f'{os.path.dirname(__file__)}/.config/service_account.json'))credentials = service_account.Credentials.from_service_account_info(service_account_info)# API Keywith open(f'{os.path.dirname(__file__)}/.config/.apikey') as f:    API_KEY = f.read()HEADERS = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': f'Bearer {credentials}'}# Construct the URLURL = 'https://www.googleapis.com/youtube/v3/subscriptions'# ParametersPARAMS = {'part':'id', 'maxResults':'250', 'order':'alphabetical', 'mine':'true', 'key': API_KEY}# Make the requestrequest = requests.get(URL, headers=HEADERS, params=PARAMS)response = request.json()# Print the responseprint(json.dumps(response, indent=4))

But I'm getting this error:

{"error": {"code": 401,"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","errors": [            {"message": "Invalid Credentials","domain": "global","reason": "authError","location": "Authorization","locationType": "header"            }        ],"status": "UNAUTHENTICATED"    }}

I'm fairly confident that the problem is in how I'm handling the credentials but I don't know how that is supposed to go.

I appreciate your input and help.

All I'm trying to do is list my own YouTube Channel Subscriptions.

Thanks!


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>