Python 检索谷歌网站';使用OAuth 2.0和服务帐户的s域索引提要
我需要开始抓取这个url:使用python脚本 如何使用OAuth2.0和服务帐户授权此Google站点URL 在OAuth1.0的情况下,我们将请求发送到并提取作为令牌接收并授权url的令牌 OAuth 1.0认证Python 检索谷歌网站';使用OAuth 2.0和服务帐户的s域索引提要,python,oauth-2.0,google-oauth,google-sites,Python,Oauth 2.0,Google Oauth,Google Sites,我需要开始抓取这个url:使用python脚本 如何使用OAuth2.0和服务帐户授权此Google站点URL 在OAuth1.0的情况下,我们将请求发送到并提取作为令牌接收并授权url的令牌 OAuth 1.0认证 url = 'https://www.google.com/accounts/ClientLogin' request = urllib.urlencode({ 'accountType': 'HOSTED', 'Email': 'admin@dmian.com',
url = 'https://www.google.com/accounts/ClientLogin'
request = urllib.urlencode({
'accountType': 'HOSTED',
'Email': 'admin@dmian.com',
'Passwd': 'userPassword',
'service': 'jotspot'})
#.. Fetch the url: https://www.google.com/accounts/ClientLogin and extract the token
headers = {
'Authorization': 'GoogleLogin auth=' + token,
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729)',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Host': 'sites.google.com',
'Connection': 'keep-alive'
}
# .. Fetch the Google Site url with below headers
不久前,我为自己编写了一个类,用GoogleAPI处理OAuth2身份验证。它可以作为你的榜样。而且,是的,您需要注册一个“应用程序”,但这只是为了获得客户id/密码 一些注意事项:
- 该类使用“脱机”访问类型来获取凭据,这些凭据可以存储并在将来重复使用
变量保存一个类的实例,该类存储我的所有设置,在本例中,是以前获得的google api凭据settings
是一个向用户提供用户/密码登录并读取谷歌生成的代码的对话框GAuthDialog
方法包装了需要访问API和身份验证的任何方法execute
self._gapi = GApi(settings, 'https://www.googleapis.com/auth/drive.readonly', 'drive', 'v2')
self._gapi.execute(self.get_file_list)
然后我们有:
def get_file_list(self):
query="query"
children = self._gapi.service.files().list(q=query).execute()
下面是课程的代码:
from oauth2client.client import OAuth2WebServerFlow, Credentials, AccessTokenRefreshError
from apiclient.discovery import build
from googleapiclient.errors import HttpError
import httplib2
class GApi():
class CredentialsError(Exception):
pass
# App credentials from developers console
__client_id = ''
__client_secret = ''
# Redirect URI for installed (non-web) apps
__redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
def __init__(self, settings, scopes, service_name, version):
self.__settings = settings
self.__scopes = scopes
self.__service_name = service_name
self.__version = version
self.__service = None
self.__credentials = None
# Try restoring credentials from settings
if self.__settings.get_gapi_credentials(self.__service_name):
self.__credentials = Credentials.new_from_json(
self.__settings.get_gapi_credentials(self.__service_name))
@property
def service(self):
return self.__service
def execute(self, method, *args, **kwargs):
self.__setup()
try:
return method(*args, **kwargs)
except AccessTokenRefreshError:
pass # Will re-authenticate below
except HttpError as err:
# Rethrow since HttpError has a bug in str()
raise Exception("Response: %s, Content: %s" %
(str(err.resp), str(err.content)))
# Try re-authenticating
self.__reauthenticate()
try:
return method(**kwargs)
except HttpError as err:
# Rethrow since HttpError has a bug in str()
raise Exception("Response: %s, Content: %s" %
(str(err.resp), str(err.content)))
def __obtain_credentials(self):
# Initialize the flow
flow = OAuth2WebServerFlow(self.__client_id, self.__client_secret,
self.__scopes, redirect_uri=self.__redirect_uri)
flow.params['access_type'] = 'offline'
# Run through the OAuth flow and retrieve credentials
uri = flow.step1_get_authorize_url()
# Get code from dialog
dialog = GAuthDialog(uri)
if dialog.exec() == QtWidgets.QDialog.Accepted and dialog.auth_code:
# Get the new credentials
self.__credentials = flow.step2_exchange(dialog.auth_code)
# Set them in settings
self.__settings.set_gapi_credentials(
self.__service_name, self.__credentials.to_json())
else:
self.__credentials = None
self.__settings.set_gapi_credentials(self.__service_name, None)
def __reauthenticate(self):
self.__credentials = None
self.__service = None
self.__setup()
def __setup(self):
# Do we have credentials?
if not self.__credentials:
self.__obtain_credentials()
# Check if we got credentials
if self.__credentials:
# Do we have service?
if not self.__service:
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = self.__credentials.authorize(http)
self.__service = build(self.__service_name,
self.__version, http=http)
else:
raise GApi.CredentialsError
这个问题似乎很相关。在那里查看我的答案:由于这个问题问得更清楚,我认为这可能是一个更好的答案:),所以在这里添加它。但是上面的代码是为了授权谷歌API。我只想授权我在问题中给出的URL。