如何使用OAuth、python登录到站点?
所以我有一个脚本,需要访问一个需要通过OAuth登录的网站的功能 我已经从开发控制台获得了一个OAuth客户端机密文件——现在我的脚本可以成功地检索用户的youtube视频列表——这就是我现在拥有的如何使用OAuth、python登录到站点?,python,oauth,oauth-2.0,youtube-api,Python,Oauth,Oauth 2.0,Youtube Api,所以我有一个脚本,需要访问一个需要通过OAuth登录的网站的功能 我已经从开发控制台获得了一个OAuth客户端机密文件——现在我的脚本可以成功地检索用户的youtube视频列表——这就是我现在拥有的 __author__ = 'anish' import httplib2 import sys from googleapiclient.discovery import build from oauth2client.client import flow_from_clientsecrets
__author__ = 'anish'
import httplib2
import sys
from googleapiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
import requests
from requests_oauthlib import OAuth1Session
# Most of the code is sourced from https://developers.google.com/youtube/v3/guides/uploading_a_video
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = "Missing client secrets"
def get_credentials(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SCOPE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
return credentials
def videos(youtube, uid):
initial = youtube.playlistItems().list(part="snippet", maxResults=50,
playlistId="UUy2Lh8b-y0r-35LlcjOPPzw").execute()
yield initial
while "nextPageToken" in initial:
initial = youtube.playlistItems().list(part="snippet", maxResults=50,
playlistId="UUy2Lh8b-y0r-35LlcjOPPzw",
pageToken=initial["nextPageToken"]).execute()
yield initial
if __name__ == "__main__":
video_list = []
http = get_credentials(argparser.parse_args()).authorize(httplib2.Http())
rq = http.request("http://github.com", method="GET")
print(rq[1])
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=http)
info = youtube.channels().list(part="contentDetails", forUsername="IndianaRobotics").execute()
uploads_id = info["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"]
for v in videos(youtube, uploads_id):
for item in v["items"]:
video_list.append([item["snippet"]["title"], item["snippet"]["resourceId"]["videoId"]])
for v_info in video_list:
print(v_info)
因此,我的访问令牌谷歌API的作品。然而,当我做这样的事情时
print(http.request("http://github.com", method="GET")
虽然我使用OAuth登录github,并且我拥有的凭据来自我的帐户,但我还是得到了登录页面作为响应
有没有办法使用我必须登录到github这样的站点的
http
对象?我需要在没有来自站点本身的任何API的情况下访问它,因为我想要与之交互的站点没有OAuth的API,而您可以使用oauth2client
获取实现OAuth2(包括github)的站点的凭据,这对于没有API或实现OAuth2的站点是不起作用的。