Notice: Welcome To Explore Information Website
IMG-LOGO
Home Blog Youtube API uses proxy proxy

Youtube API uses proxy proxy

by Technical otaku - 2022-09-17 2177 0 2

Through the YouTube API , we can easily get information such as YouTube playlists, videos and categories. But the premise is to first apply for a DEVELOPER_KEY, the application address: https://console.cloud.google.com/apis/credentials.

However, in China, you cannot directly connect to YouTube, nor can you directly connect to the YouTube API. The following is an example of obtaining video information, the code:

from googleapiclient.discovery import buildYOUTUBE_DEVELOPER_KEY = 'Enter your DEVELOPER_KEY'YOUTUBE_API_SERVICE_NAME = 'youtube'YOUTUBE_API_VERSION = 'v3'youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=YOUTUBE_DEVELOPER_KEY)result = youtube.videos().list(
    part="snippet",
    id="HtSuA80QTyo").execute()print(result)

The above is the method without proxy. There are two ways to use proxy:

Method one specifies httpparameters

from googleapiclient.discovery import buildimport httplib2from httplib2 import socksYOUTUBE_DEVELOPER_KEY = 'Enter your DEVELOPER_KEY'YOUTUBE_API_SERVICE_NAME = 'youtube'YOUTUBE_API_VERSION = 'v3'# Here are the proxy settingsproxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)http = httplib2.Http(timeout=config.youtube_api_timeout, proxy_info=proxy_info)# Increase the http parameter when building the youtube objectyoutube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=YOUTUBE_DEVELOPER_KEY, http=http)result = youtube.videos().list(
    part="snippet",
    id="HtSuA80QTyo").execute()print(result)

Method 2 Set up socket proxy

from httplib2 import socksimport socketfrom googleapiclient.discovery import build# Set the socket proxy, which is equivalent to using the proxy at the bottom layer, and does not need to be set in the youtube parameterssocks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)socket.socket = socks.socksocketYOUTUBE_DEVELOPER_KEY = 'Enter your DEVELOPER_KEY'YOUTUBE_API_SERVICE_NAME = 'youtube'YOUTUBE_API_VERSION = 'v3'youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=YOUTUBE_DEVELOPER_KEY)result = youtube.videos().list(
    part="snippet",
    id="HtSuA80QTyo").execute()print(result)


Tags:

0 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *