2013-03-14 49 views
0

我对Pear :: Auth PHP库有个疑问。我可以以某种方式“在命令行中登录”。我的意思是我可以访问通过Pett :: Auth从HttpRequest保护的资源吗?我在其他脚本程序中创建自己?你可以给我一个示例(Python,PHP,Java或任何东西可读)Pear :: Auth从命令行访问

回答

0

好,我知道它的工作(希望这可以帮助别人 - 这是Python的解决方案,可转换为任何语言,并允许你通过创建的HttpRequest你在你的程序的控制登录PEAR ::验证

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 
0

实际上取决于...什么,并从那里你正在试图做到这一点...我想到有些事情:

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

谢谢...我有点意识到它后来...在解决方案上工作。我半... ...无论如何。将完成后发布 – kosta5 2013-03-14 18:31:05

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的将其保持其存在的整个时间,所以你不需要担心THA t)