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

Youtube Analytics API OAuth 2.0 authentication issue. The authentication page doesn't load forever

$
0
0

This is my script, and I am using the same localhost in my API OAuth as well. What is happening?

The page remains stuck on this verification forever without ever picking the Key:

The page remains stuck on this verification forever without ever picking the Key

We're attempting to generate a link for authentication to my google account for verification.The script fetches daily analytics (views, likes, dislikes, average view duration) from the YouTube Analytics API for a specified YouTube channel. It uses OAuth2 credentials from a credentials.json file, and if necessary, guides the user through the authentication process. The retrieved analytics data is then saved to a CSV file named youtube_analytics.csv.

import jsonimport osimport pickleimport csvfrom datetime import datefrom google_auth_oauthlib.flow import InstalledAppFlowfrom googleapiclient.discovery import buildfrom google.auth.transport.requests import Request# os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'# credswith open("credentials.json", "r") as f:    config = json.load(f)SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']def get_credentials():    creds = None    if os.path.exists('token.pickle'):        with open('token.pickle', 'rb') as token:            creds = pickle.load(token)    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("credentials.json", SCOPES)            flow.redirect_uri = 'http://localhost:5000/'            auth_url, _ = flow.authorization_url(prompt='consent')            print(f"Please go to this URL: {auth_url}")            auth_response = input('Enter the full callback URL: ')            flow.fetch_token(authorization_response=auth_response)            creds = flow.credentials            with open('token.pickle', 'wb') as token:                pickle.dump(creds, token)    return credsdef fetch_youtube_analytics(channel_id):    creds = get_credentials()    youtube_analytics = build('youtubeAnalytics', 'v2', credentials=creds)    today = date.today().strftime('%Y-%m-%d')    response = youtube_analytics.reports().query(        ids=f"channel=={channel_id}",        startDate=today,        endDate=today,        metrics="views,likes,dislikes,averageViewDuration",        dimensions="day",        sort="day"    ).execute()    return responsedef save_to_csv(data, filename="youtube_analytics.csv"):    headers = ["Date", "Views", "Likes", "Dislikes", "Average View Duration"]    rows = data.get('rows', [])    with open(filename, 'w', newline='') as csv_file:        writer = csv.writer(csv_file)        writer.writerow(headers)        writer.writerows(rows)if __name__ == "__main__":    channel_id = "XYZ12345"    analytics_data = fetch_youtube_analytics(channel_id)    save_to_csv(analytics_data)    print(f"Data saved to youtube_analytics.csv")

Trying to fetch Analytics using YouTube Analytics API from my YouTube channel.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



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