任何人都可以提供示例代码,以替代使用现已解散的gdata.docs.client API的代码:
import gdata.docs, gdata.docs.client, gdata
pw = file('pw','r').read()
client = gdata.docs.client.DocsClient()
client.ClientLogin('username@domain.org',pw,None)
直到昨天,该代码仍然有效,但谷歌终于破解了已降级的ClientLogin API(https://developers.google.com/identity/protocols/AuthForInstalledApps)。
在阅读OAuth 2.0库的文档时,看起来该过程应该涉及用户交互以完成身份验证过程,但我需要一个脚本在cronjob中定期运行而无需用户参与(我们使用cronjob上的脚本更新我们谷歌网站的各个部分。
当前答案:
docs API的硬编码认证是可能的,但该API也已停止使用,因此这是为新的首选DRIVE API执行此操作的方法。
credentials = OAuth2WebServerFlow(
client_id = "CLIENT ID",
client_secret = "CLIENT SECRET",
scope = 'https://www.googleapis.com/auth/drive',
user_agent = "HTTP",
access_token = "ACCESS TOKEN",
refresh_token = "REFRESH TOKEN",
)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
# Now it gets ugly
# The authorize method below changes the "request" method of http_client
# Not at all sure why I had to fake this to make it work, but I did --
# I believe this must get set somewhere in the normal gdrive flow which
# we're circumventing with this method. You can see the code that
# requires this fake client here:
# https://code.google.com/p/gdata-python client/source/browse/src/gdata/gauth.py
# at ~line 1324
Class FakeClient:
request = 'Fake'
http.http_client = FakeClient()
http = credentials.authorize(http)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
Class FakeClient:
request = 'Fake'
http.http_client = FakeClient()
http = credentials.authorize(http)
要获取这些凭据,您可以使用Google文档中描述的标准OAuth方法,然后深入了解变量以查找所有正确的信息。这是我自己编写的一些代码,用于打印出来:
if NEED_NEW_CREDENTIALS:
CLIENT_ID = 'ID'
CLIENT_SECRET = 'SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE,
redirect_uri=REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
print 'Hey buddy -- you better hardcode these new credentials in.'
print 'client_id = "%s"'%credentials.client_id
print 'client_secret = "%s"'%credentials.client_secret
print 'scope = "%s"'%OAUTH_SCOPE
print 'user_agent = "%s"'%credentials.user_agent
print 'access_token = "%s"'%credentials.token_response['access_token']
print 'refresh_token = "%s"'%credentials.token_response['refresh_token']
答案 0 :(得分:1)
好的,我在PyDrive库中找到了一个更好的解决方案,它已经很好地为您完成了所有这些:
http://pythonhosted.org/PyDrive/
如果您将PyDrive设置为存储凭据,它只会让您浏览一次浏览器,然后自动记住凭据。
您需要设置一个看起来像这样的settings.yaml文件来处理它:
class searchingViewController: UITableViewController{
override func viewDidLoad() {
var items: [AnyObject] = ["Searching Method A", "Searching Method B"]
var searchSC:UISegmentedControl!
searchSC.frame = CGRectMake(0, 0, frame.width - 20, 30)
searchSC.selectedSegmentIndex = 1
searchSC.backgroundColor = UIColor(white: 1, alpha: 1)
searchSC.layer.cornerRadius = 5.0
navigateUIToolBar = UIToolbar(frame: CGRectMake(frame.minX + 10, ios7_8Info.getStatusBarHeight()+self.navigationController!.navigationBar.frame.height+frame.minY+(21/2),
frame.width - 20, 30))
navigateUIToolBar.addSubview(searchSC)
self.navigationController?.navigationBar.addSubview(navigateUIToolBar)
}
}
一旦你完成了,并在client_secrets.json中安装了你的秘密,登录过程就像这样简单:
save_credentials: true
save_credentials_backend: file
save_credentials_file: credentials.txt
client_config_file: client_secrets.json
然后您正在使用PyDrive API,它非常友好,并且在此处有详细记录:http://pythonhosted.org/PyDrive/oauth.html#customizing-authentication-with-settings-yaml