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

Why can I not set part equal to 'contentDetails' instead of 'snippet' for a PlaylistItems Insert method call?

$
0
0

I am very new to the YouTube API and am quite confused with the difference between the part and body of the API requests and what they mean. On the YouTube API PlaylistItems Insert documentation it mentions that it takes contentDetails, id, snippet, and status as headers. The following code used to insert a video into a playlist works fine:

import osimport picklefrom dotenv import load_dotenvfrom googleapiclient.discovery import buildfrom google_auth_oauthlib.flow import InstalledAppFlowfrom google.auth.transport.requests import Requestcredentials = None# token.pickle stores the user's credentials from previously successful loginsif os.path.exists('token.pickle'):    print('Loading Credentials From File...')    with open('token.pickle', 'rb') as token:        credentials = pickle.load(token)# If there are no valid credentials available, then either refresh the token or log in.if not credentials or not credentials.valid:    if credentials and credentials.expired and credentials.refresh_token:        print("Refreshing Access Token...")        credentials.refresh(Request())    else:        print("Fetching New Tokens...")        flow = InstalledAppFlow.from_client_secrets_file('client_secrets.json',            scopes=['https://www.googleapis.com/auth/youtube']        )        flow.run_local_server(port=8080, prompt='consent', authorization_prompt_message='')        credentials = flow.credentials        # Save the credentials for the next run        with open('token.pickle', 'wb') as f:            print("Saving Credentials for Future Use...")            pickle.dump(credentials, f)load_dotenv()API_KEY = os.getenv('API_KEY')with build('youtube', 'v3', credentials=credentials) as ytService:    request = ytService.playlistItems().insert(        part='snippet',        body={'snippet': {'playlistId': 'PLL_zqGJzFipIjbeZfTmJTgnoM4Gl-80Hn','resourceId': {'kind': 'youtube#video','videoId': 'PhPqP2qUHdg'                }            }        }        )    response = request.execute()    print(response)

However, changing part='snippet' to part='contentDetails' produces the following error:

Traceback (most recent call last):  File "d:\Documents\Programming Projects\Google YouTube API\main.py", line 52, in <module>    response = request.execute()  File "D:\Documents\Programming Projects\Google YouTube API\.venv\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper    return wrapped(*args, **kwargs)  File "D:\Documents\Programming Projects\Google YouTube API\.venv\lib\site-packages\googleapiclient\http.py", line 938, in execute    raise HttpError(resp, content, uri=self.uri)googleapiclient.errors.HttpError: <HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=contentDetails&alt=json returned "'snippet'". Details: "[{'message': "'snippet'", 'domain': 'youtube.part', 'reason': 'unexpectedPart', 'location': 'part', 'locationType': 'parameter'}]">

Why is this the case?


Viewing all articles
Browse latest Browse all 3831

Trending Articles



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