Pear::Auth PHP ライブラリについて質問があります。どういうわけか「コマンドラインでログイン」できますか。他のスクリプト プログラムで作成した HttpRequest から、Pear::Auth を介して保護されているリソースにアクセスできますか? 例を教えてください(python、php、java、または読み取り可能なもの)
3 に答える
0
OK、うまくいきました (うまくいけば、これは他の誰かを助けるかもしれません.
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
本当に依存しています...何をどこからこれをやろうとしているかに依存します...頭に浮かぶいくつかのこと:
- 資格情報をコマンド ライン引数として渡します。
- 資格情報を GET 要求パラメーターの一部として渡します (SSL を想定している可能性があります)。
- cookie jar で cURL を使用します。
- これらの呼び出しがニーズを容易にする別の方法で認証を処理するように、サービス レイヤーを実装します (既存の Pear Auth で動作する認証付き API)。
于 2013-03-14T17:59:13.020 に答える
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 "";
}
}
注: Cookie は自動的に処理されます (httpclient は、存在している間ずっと Cookie を保持するわけではないため、心配する必要はありません)。
于 2013-03-20T10:04:52.360 に答える