我試圖從Android應用程序登錄Vtiger。我成功獲取了挑戰令牌並使用訪問密鑰(從我的帳戶首選項中獲取)對挑戰令牌進行了協調。然後轉換爲MD5字符串。指定的令牌在Vtiger中無效或過期登錄
我的挑戰網址:
http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=getchallenge&username=Manager
挑戰響應:
{"success":true,"result":{"token":"4ff555f87eece","serverTime":1341478392,"expireTime":1341478692}}
登錄網址:
http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=login&username=Manager&accesskey=976b9571ff3ff92b7786da17125ac37c
登錄響應:
{"success":false,"error":{"code":"INVALID_AUTH_TOKEN","message":"Specified token is invalid or expired"}}
另外我有一個疑問,在Android中我用的AsyncTask對於HTTP Operation.Two的AsyncTask used.Fisrt一個獲得挑戰令牌,並使用此令牌完成第二異步任務登錄。這是正確的方式嗎?我的Android代碼如下。
我的Android代碼:
public class GetChallengeActivity extends Activity {
private TextView textView;
private static final String USER_NAME="Manager";
private static final String ACCESS_KEY="myaccesskey"; //taken from preferences
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void postData(View view)
{
textView=(TextView) findViewById(R.id.resulttext);
ConnectivityManager conmgr=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo=conmgr.getActiveNetworkInfo();
Log.i(null, "Network Info-->"+networkinfo.toString());
if(networkinfo!=null && networkinfo.isConnected())
{
//Network Available.
//Toast.makeText(getApplicationContext(), "Network available", Toast.LENGTH_LONG).show();
Log.i(null, "Network Available");
textView.setText("Network Available");
String url="http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=getchallenge&username="+USER_NAME+"";
Log.i(null,"Challenge URL-->"+url);
new ServerCommunication().execute(url);
}
else
{
//Network not Available
Log.i(null, "Network Not Available");
Toast.makeText(getApplicationContext(), "Network Not available", Toast.LENGTH_LONG).show();
textView.setText("Network Not Available");
}
}
private class ServerCommunication extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... urls) {
Log.i(null, "Doing in Background");
try {
return postToServer(urls[0]);
}catch (IOException e) {
Log.i(null, "Unable to retrieve web page. URL may be invalid.");
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result)
{
Log.i(null, "onPostExecute");
Log.i(null, result);
textView.setText(result);
}
private String postToServer(String string) throws IOException{
Log.i(null, "post2Server");
InputStream is = null;
// Only display the first 500 characters of the retrieved web page content.
int len = 500;
try {
URL url = new URL(string);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
Log.i(null,contentAsString);
String result="";
try {
JSONObject jsondata=new JSONObject(contentAsString);
String outcomesrf=jsondata.getString("success");
Log.i(null, "OutCome---->"+outcomesrf);
if(outcomesrf=="false")
{
JSONObject jsonobj2=jsondata.getJSONObject("error");
result=jsonobj2.getString("message");
Log.i(null,"Error-->errorMessage---->"+result);
}
else
{
JSONObject jsonobj3=jsondata.getJSONObject("result");
result=jsonobj3.getString("token");
Log.i(null,"Success-->Token---->"+result);
Log.i(null,"-->MD5Hash("+result+ACCESS_KEY+")");
String generatedkey=MD5_Hash(result+ACCESS_KEY);
Log.i(null,"Key-->"+generatedkey);
String loginurl="http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=login&username="+USER_NAME+"&accesskey="+generatedkey+"";
Log.i(null,"Login URL-->"+loginurl);
new LoginCommunication().execute(loginurl);
}
} catch (JSONException e) {
e.printStackTrace();
Log.i(null,"At Json "+e.toString());
}
return result;
}
finally {
if (is != null) {
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
}
private class LoginCommunication extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... urls) {
Log.i(null, "Login Posting in Background");
try {
return loginToServer(urls[0]);
}catch (IOException e) {
Log.i(null, "Unable to retrieve web page. URL may be invalid.");
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result)
{
Log.i(null, "LoginonPostExecute");
Log.i(null, result);
textView.setText(result);
}
private String loginToServer(String string) throws IOException{
Log.i(null, "loginToServer");
InputStream is = null;
int len = 500;
try {
URL url = new URL(string);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
Log.i(null,contentAsString);
String result1="";
String result2="";
try {
JSONObject jsondata=new JSONObject(contentAsString);
String outcomesrf=jsondata.getString("success");
Log.i(null, "Login OutCome---->"+outcomesrf);
if(outcomesrf=="false")
{
JSONObject jsonobj2=jsondata.getJSONObject("error");
result1=jsonobj2.getString("message");
Log.i(null,"Error-->errorMessage---->"+result1);
}
else
{
JSONObject jsonobj3=jsondata.getJSONObject("result");
result1=jsonobj3.getString("sessionName");
Log.i(null,"Success-->sessionName---->"+result1);
JSONObject jsonobj4=jsondata.getJSONObject("result");
result2=jsonobj4.getString("userId");
Log.i(null,"Success-->userId---->"+result2);
}
} catch (JSONException e) {
e.printStackTrace();
Log.i(null,"At Json "+e.toString());
}
String result="sessionName"+result1+" userId"+result2;
return result;
}
finally {
if (is != null) {
is.close();
}
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
public static String MD5_Hash(String s) {
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(s.getBytes(),0,s.length());
String hash = new BigInteger(1, m.digest()).toString(16);
return hash;
}
}
請任何人都可以幫助... – Ramprasad 2012-07-07 09:47:41
@Ramprasad喜我正在創建一個簡單的應用程序來僅顯示來自crm服務器的線索。我找不到有關如何正確進行登錄活動的任何適當文檔。如果我只需使用accessKey登錄,那麼我應該如何使用用戶名密碼登錄?官方vtiger crm客戶和其他人有用戶名和密碼登錄。我很困惑繼續前進。你能幫我 – 2015-05-10 09:41:04