0

我对 Pear::Auth PHP 库有疑问。我可以以某种方式“登录命令行”吗?我的意思是我可以从 HttpRequest 访问受 Pear::Auth 保护的资源吗?我在其他脚本程序中创建自己?你能给我一个例子吗(python、php、java或任何可读的)

4

3 回答 3

0

真的取决于...取决于您尝试执行此操作的内容和位置...想到的一些事情:

  • 将凭据作为命令行参数传递。
  • 将凭据作为 GET 请求参数的一部分传递(可能假设为 SSL)。
  • 将 cURL 与 cookie 罐一起使用。
  • 实现一个服务层,以便这些调用以一种可满足您需求的替代方式处理身份验证(具有身份验证的 API 可与您现有的 Pear Auth 一起使用)。
于 2013-03-14T17:59:13.020 回答
0

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

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
于 2013-03-14T18:52:30.087 回答
0

如果有人对这里的 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 "";
    }
}

注意:cookies 是自动处理的(httpclient 在它存在的整个过程中都会保存它们,所以你不必担心)

于 2013-03-20T10:04:52.360 回答