Pear::命令行授权访问


Pear::Auth access from command line

我有一个关于Pear::Auth PHP库的问题。我可以以某种方式"登录命令行"。我的意思是我可以访问一个资源,通过梨::Auth从HttpRequest我创建自己在其他一些脚本程序?你能给我举个例子吗(python, php, java或任何可读的)

这取决于…关于你想做什么,从哪里做……想到的一些事情:

  • 将凭证作为命令行参数传递。
  • 传递凭据作为GET请求参数的一部分(可能假设使用SSL)。
  • 使用cURL与饼干罐。
  • 实现一个服务层,以便这些调用以另一种方式处理身份验证,以方便您的需求(具有身份验证的API与您现有的Pear Auth一起工作)。

Ok,让它工作(希望这可能会帮助别人-这是Python的解决方案,可以转换为任何语言,并允许您登录到Pear::Auth通过创建HttpRequest您的控制在您的程序。

import urllib
import httplib2
from urllib import urlencode
http = httplib2.Http()
url = 'LOGIN_URL'
# this applies to current version of Pear (had to add the authsecret)
body = {'username': 'USRENAME', 'password': 'PASSWORD', 'authsecret': ''} 
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))
# CAREFUL!!! HERE IS THE TRICKY PART !
# response['set-cookie'] looks like this -> 'SomeSession=longID; path=/, SomeSession=longID; path=/, authchallenge=blabla; path=/'
# you need to parse it and use only authchallanage and SomeSessionpart(just one of them I think the last one) - have no idea why, but it works. So next line needs some parsing and fixing
headers = {'Cookie': response['set-cookie']} #parseme -- this is not correnct and will not work, but you have to fix it to match your implementation
print headers
# e.g. headers = {'Cookie': 'MySession=bdfdstiq90oilkpk7n4s2q2g50; authchallenge=fddtggffg5784d359c12dfad4059', 'X_REQUESTED_WITH': 'xmlhttprequest'}#the second header is optional, I needed to access some ajax call
data = dict(argument="to_pass", eg="customerID")
resp, content = http.request("secure_URL", "POST", urlencode(data), headers=headers)
print resp
print content

如果有人对Java解决方案感兴趣,这里是:

public String getServerJson(){
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost("https://"+hostToReach+"/secure/index.php");
    httpost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("username", htmlUsername));
    nvps.add(new BasicNameValuePair("password", htmlPassword));
    httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
    try {
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    HttpPost ajaxPost = new HttpPost("https://"+hostToReach+"/?event_id="+pmp.getPmpEventId().getEventId().toString()+"&categories_only=true&pos=true");
    ajaxPost.setHeader("X_REQUESTED_WITH", "xmlhttprequest");
    try {
        HttpResponse catResponse = httpclient.execute(ajaxPost);
        BufferedReader rd = new BufferedReader (new InputStreamReader(catResponse.getEntity().getContent()));
        String line = "";
        String json = "";
        while ((line = rd.readLine()) != null) {
              json += line;
        }
        EntityUtils.consume(catResponse.getEntity());
        return json;

    }
    catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

注意:cookie是自动处理的(httpclient在其存在的整个时间内都持有它们,所以你不需要担心)