2013-03-14 4 views
0

Pear :: Auth PHP 라이브러리에 대한 질문이 있습니다. 어떻게 든 "명령 줄에서 로그인"할 수 있습니까? 나는 HearpRequest로부터 Pear :: Auth를 통해 보호받는 리소스에 액세스 할 수 있다는 것을 의미한다. 나는 다른 스크립트 프로그램에서 자신을 만든다. 그리고 당신은 내게에 대한 예제 (파이썬, PHP, 자바 또는 읽을 것을)Pear :: Auth access from command line

답변

0

좋아, 희망이 다른 사람 도움이 될 수 있습니다 (작업 그것을 가지고 -.이 모든 언어로 변환하고 프로그램에서 생성 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 
0

정말 달려 ...을 줄 수있는 당신은 마음에 와서이 ... 어떤 일을하려고하는 곳에서 :

  • 자격 증명을 명령 줄 인수로 전달하십시오.
  • GET 요청 매개 변수의 일부로 자격 증명을 전달합니다 (아마 SSL로 가정).
  • 쿠키 병이있는 cURL을 사용하십시오.
  • 서비스 레이어를 구현하여 이러한 호출이 기존 배 인증과 함께 작동하는 인증을 사용하여 사용자의 필요를 용이하게하는 대체 방식으로 인증을 처리하도록합니다.
+0

감사합니다 ... 나중에 다소 시간을 깨달았습니다 ... 해결책을 찾고 있습니다. 나는 반쯤 가지고있다. .. 고마워. 한 번 끝나면 여기에 올리겠습니다. – kosta5

0

여기있다 :

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 ""; 
    } 
} 

주 : 쿠키가 자동으로 처리된다 (당신이 그쪽에 대해 걱정할 필요가없는, 그래서 HttpClient를, 그 존재의 전체 시간을 보유하고 t)