【发布时间】:2015-11-17 07:48:09
【问题描述】:
我有一个登录名和密码来连接到一个平台。从浏览器登录后,我将获得一个 access_token,它存储在 cookie 中(我猜)。如何使用 PHP(可能使用 cURL)以编程方式获取访问令牌?
我找到了一个解决方案,但它是用 python 编写的,如何用 PHP 做同样的事情?
这里是python的解决方案
import urllib
import urllib2
import cookielib
import json
import logging
class GISTokenGenerator:
def __init__(self, email, password):
self.cj = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
self.email = email
self.login_data = urllib.urlencode({'user[email]': email, 'user[password]': password})
def generate_token(self):
logging.info('Generating a token for {0}...'.format(self.email))
self.opener.open('https://site/users/sign_in', self.login_data)
for cookie in self.cj:
if cookie.name == 'aiesec_token':
token_json = json.loads(urllib.unquote(cookie.value))
token = token_json['token']['access_token']
if token is None:
raise Exception('Unable to generate a token for {0}!'.format(self.email))
return token
【问题讨论】:
-
如果访问令牌存储在 cookie 中(您听起来不确定——是吗?您能检查一下吗?),为什么要使用 cURL 来访问 cookie?
标签: php python rest curl cookies