me yaboi
me yaboi

Reputation: 31

I am trying to upload a youtube video using the Youtube API with python

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "C:/Users/virto/Desktop/Projekt/client_Secrets.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.videos().insert(
        part="snippet,status",
        body={
          "snippet": {
            "categoryId": "22",
            "description": "",
            "title": "Memes Compilation."
          },
          "status": {
            "privacyStatus": "public"
          }
        },
        
        # TODO: For this request to work, you must replace "YOUR_FILE"
        #       with a pointer to the actual file you are uploading.
        media_body=MediaFileUpload("C:/Users/virto/Desktop/Projekt/Memes Compilation.mp4")
    )
    response = request.execute()

    print(response)

This is my code. I am getting an error message as follows.


SSLWantWriteError                         Traceback (most recent call last)
<ipython-input-1-d6d6f2ce7763> in <module>
    102         os.remove('C:/Users/virto/Desktop/Projekt/' + str(i))
    103 
--> 104 Final()
    105 

<ipython-input-1-d6d6f2ce7763> in Final()
     97     Schnitt()
     98     print('Upload starting')
---> 99     main()
    100     print('Löschen...')
    101     for i in Memes:

<ipython-input-1-d6d6f2ce7763> in main()
     87         media_body=MediaFileUpload("C:/Users/virto/Desktop/Projekt/Memes Compilation.mp4")
     88     )
---> 89     response = request.execute()
     90 
     91     print(response)

~\AppData\Roaming\Python\Python38\site-packages\googleapiclient\_helpers.py in positional_wrapper(*args, **kwargs)
    132                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    133                     logger.warning(message)
--> 134             return wrapped(*args, **kwargs)
    135 
    136         return positional_wrapper

~\AppData\Roaming\Python\Python38\site-packages\googleapiclient\http.py in execute(self, http, num_retries)
    890 
    891         # Handle retries for server-side errors.
--> 892         resp, content = _retry_request(
    893             http,
    894             num_retries,

~\AppData\Roaming\Python\Python38\site-packages\googleapiclient\http.py in _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs)
    202         if exception:
    203             if retry_num == num_retries:
--> 204                 raise exception
    205             else:
    206                 continue

~\AppData\Roaming\Python\Python38\site-packages\googleapiclient\http.py in _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs)
    175         try:
    176             exception = None
--> 177             resp, content = http.request(uri, method, *args, **kwargs)
    178         # Retry on SSL errors and socket timeout errors.
    179         except _ssl_SSLError as ssl_error:

~\anaconda3\lib\site-packages\google_auth_httplib2.py in request(self, uri, method, body, headers, **kwargs)
    198 
    199         # Make the request.
--> 200         response, content = self.http.request(
    201             uri, method, body=body, headers=request_headers, **kwargs)
    202 

~\AppData\Roaming\Python\Python38\site-packages\httplib2\__init__.py in request(self, uri, method, body, headers, redirections, connection_type)
   1983                     content = b""
   1984                 else:
-> 1985                     (response, content) = self._request(
   1986                         conn,
   1987                         authority,

~\AppData\Roaming\Python\Python38\site-packages\httplib2\__init__.py in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1648             auth.request(method, request_uri, headers, body)
   1649 
-> 1650         (response, content) = self._conn_request(
   1651             conn, request_uri, method, body, headers
   1652         )

~\AppData\Roaming\Python\Python38\site-packages\httplib2\__init__.py in _conn_request(self, conn, request_uri, method, body, headers)
   1556                 if conn.sock is None:
   1557                     conn.connect()
-> 1558                 conn.request(method, request_uri, body, headers)
   1559             except socket.timeout:
   1560                 conn.close()

~\anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
   1238                 encode_chunked=False):
   1239         """Send a complete request to the server."""
-> 1240         self._send_request(method, url, body, headers, encode_chunked)
   1241 
   1242     def _send_request(self, method, url, body, headers, encode_chunked):

~\anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             # default charset of iso-8859-1.
   1285             body = _encode(body, 'body')
-> 1286         self.endheaders(body, encode_chunked=encode_chunked)
   1287 
   1288     def getresponse(self):

~\anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked)
   1233         else:
   1234             raise CannotSendHeader()
-> 1235         self._send_output(message_body, encode_chunked=encode_chunked)
   1236 
   1237     def request(self, method, url, body=None, headers={}, *,

~\anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked)
   1043                     chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
   1044                         + b'\r\n'
-> 1045                 self.send(chunk)
   1046 
   1047             if encode_chunked and self._http_vsn == 11:

~\anaconda3\lib\http\client.py in send(self, data)
    965             return
    966         try:
--> 967             self.sock.sendall(data)
    968         except TypeError:
    969             if isinstance(data, collections.abc.Iterable):

~\anaconda3\lib\ssl.py in sendall(self, data, flags)
   1202                 amount = len(byte_view)
   1203                 while count < amount:
-> 1204                     v = self.send(byte_view[count:])
   1205                     count += v
   1206         else:

~\anaconda3\lib\ssl.py in send(self, data, flags)
   1171                     "non-zero flags not allowed in calls to send() on %s" %
   1172                     self.__class__)
-> 1173             return self._sslobj.write(data)
   1174         else:
   1175             return super().send(data, flags)

SSLWantWriteError: The operation did not complete (write) (_ssl.c:2457)

I would really appreciate your help. In my youtube account there is a youtube video post which was made after running this code. Sadly its upload is at 0% and is uncomplete. I cut the error message in order to post this .

Upvotes: 1

Views: 560

Answers (1)

stvar
stvar

Reputation: 6985

As already mentioned in one of my comments above, I suspect that you're running your program on a computer that has at least two Python installations -- each set up with own (separate) Google API Client Library for Python packages --, and that your program is actually running an old version of the client library.

I suggest to verify the version of the client library at run-time. Do that by inserting the following piece of code right at the beginning of main function:

print('google-api-python-client version:',
       get_googleapiclient_version())

where the function get_googleapiclient_version is:

def get_googleapiclient_version():
    try:
        import googleapiclient
        return googleapiclient.__version__
    except AttributeError:
        import pkg_resources
        return pkg_resources.get_distribution(
              "google-api-python-client").version

The version numbers shown should be newer than 1.6.7 from Apr 27, 2018.

The best would always be to make sure you're running the latest version. (At the time of this writing that is 1.10.0 from Jul 15th.)


A caveat of function get_googleapiclient_version above: using pkg_resources is not one hundred percent a reliable method for obtaining a package's version. In any case, the following piece of code prints the path of googleapiclient:

print('google-api-python-client path:',
       googleapiclient.__path__)

This may help troubleshoot the real origin of the client library your program is actually running.

Upvotes: 1

Related Questions