I am in China. I use a proxy to connect to the internet and when I want to proxy a program, I tunnel it with proxychains. Now, the question: I have this code, which is a simple auth against the YouTube API:
import httplib2
import os
import logging
from oauth2client import tools
from oauth2client.client import AccessTokenCredentials
#from oauth2client.client import AccessTokenRefreshError
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from googleapiclient.errors import HttpError
import urllib
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def authenticate():
httplib2.debuglevel = 4
acc_token = "ya29.dgLFP1i6jTuc-hnaC9D704i2jbQ2HOHweSqxjL9GxSFBg8QgvU"
user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
flow = AccessTokenCredentials(acc_token, user_agent)
http = flow.authorize(httplib2.Http())
service = build('youtube', 'v3', http=http)
return(service)
def initialize_upload(youtube):
tags = 'classical music', 'yehudi mehunin'
body = dict(
snippet=dict(
title='some title',
description='a description',
tags=tags,
categoryId='4'
),
status=dict(
privacyStatus='Private'
)
)
youtube.videos().insert(part=",".join(body.keys()), body=body, media_body=MediaFileUpload(
'1977.mp4', mimetype='video/mp4', chunksize=1024 * 1024, resumable=False))
def first():
youtube = authenticate()
initialize_upload(youtube)
first()
When I first turn on my computer, I activate my virtualenv, execute the script form a terminal without proxying it, and I get a timeout (I have to break it manually to exit) and I get this output:
^CTraceback (most recent call last):
File "youtubeconnect.py", line 48, in <module>
first()
File "youtubeconnect.py", line 45, in first
youtube = authenticate()
File "youtubeconnect.py", line 21, in authenticate
service = build('youtube', 'v3', http=http)
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/oauth2client-1.5.2-py3.5.egg/oauth2client/util.py", line 140, in positional_wrapper
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/google_api_python_client-1.4.2-py3.5.egg/googleapiclient/discovery.py", line 196, in build
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/google_api_python_client-1.4.2-py3.5.egg/googleapiclient/discovery.py", line 242, in _retrieve_discovery_doc
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/oauth2client-1.5.2-py3.5.egg/oauth2client/client.py", line 596, in new_request
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 1314, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 1064, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 987, in _conn_request
conn.connect()
File "/usr/lib/python3.5/http/client.py", line 1229, in connect
super().connect()
File "/usr/lib/python3.5/http/client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
KeyboardInterrupt
Now, I run it for the first time tunneled with proxychains and I get a response:
|DNS-request| www.googleapis.com
|S-chain|-<>-127.0.0.1:1080-<><>-4.2.2.2:53-<><>-OK
|DNS-response| www.googleapis.com is 74.125.29.95
|S-chain|-<>-127.0.0.1:1080-<><>-74.125.29.95:443-<><>-OK
send: b'GET /discovery/v1/apis/youtube/v3/rest HTTP/1.1\r\nHost: www.googleapis.com\r\nuser-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36\r\nauthorization: Bearer ya29.dgLFP1i6jTuc-hnaC9D704i2jbQ2HOHweSqxjL9GxSF\r\naccept-encoding: gzip, deflate\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Expires header: Date header: ETag header: Vary header: Vary header: Content-Type header: Content-Encoding header: X-Content-Type-Options header: X-Frame-Options header: X-XSS-Protection header: Server header: Content-Length header: Age header: Cache-Control header: Alternate-Protocol header: Alt-Svc (venv) xavier@xavier:~/Code/autotube$ proxychains python3 youtubeconnect.py
ProxyChains-3.1 (http://proxychains.sf.net)
Now, why when I run it again, both tunneling it and not tunneling it, the script executes and doesn't give any output anymore? The script executes without error and that's it. No output. I can only get output when I restart my computer. Is the API or some library using a cache or something similar? I have also tried deactivating and reactivating the venv, but everything remains the same. Does someone know?
Ok, it has to do with how httplib2 handles the cache. It has automatic caching enabled, so in order to get a copy of the response you have to specify a cache directory in the httplib2.Http('.cache') object. There is no way to avoid automatic caching if you don't specifically modify the cache headers in order to ignore the cache and send the request again to the server, bypassing it. Bear in mind that if you are proxying, your proxy has also a cache. For good information about HTTP, httplib2 and Python3, check the free resource : http://www.diveintopython3.net/http-web-services.html