android 融雲 獲取token

 

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.jieting.shangmen.bean.UserRespone;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jieting on 2019/2/24.
 */

public class GetToken {


    public static String GetRongCloudToken(String username) {
        StringBuffer res = new StringBuffer();
        String url = "https://api.cn.ronghub.com/user/getToken.json";
        String App_Key = " "; //開發者平臺分配的 App Key。
        String App_Secret = " ";
        String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//時間戳,從 1970 年 1 月 1 日 0 點 0 分 0 秒開始到現在的秒數。
        String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//隨機數,無長度限制。
        String Signature = sha1(App_Secret + Nonce + Timestamp);//數據簽名。
        Log.e("GetToken",Signature);
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("App-Key", App_Key);
        httpPost.setHeader("Timestamp", Timestamp);
        httpPost.setHeader("Nonce", Nonce);
        httpPost.setHeader("Signature", Signature);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("userId",username));
        HttpResponse httpResponse = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));
            httpResponse = httpClient.execute(httpPost);
            BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String line = null;
            while ((line = br.readLine()) != null) {
                res.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        UserRespone userRespone = JSON.parseObject(res.toString(), UserRespone.class);
        Log.e("GetToken",userRespone.getCode()+"");
        return userRespone.getToken();
    }
    //SHA1加密//http://www.rongcloud.cn/docs/server.html#通用_API_接口簽名規則
    private static String sha1(String data){
        StringBuffer buf = new StringBuffer();
        try{
            MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(data.getBytes());
            byte[] bits = md.digest();
            for(int i = 0 ; i < bits.length;i++){
                int a = bits[i];
                if(a<0) a+=256;
                if(a<16) buf.append("0");
                buf.append(Integer.toHexString(a));
            }
        }catch(Exception e){

        }
        return buf.toString();
    }

}

 

轉自:https://blog.csdn.net/lwqldsyzx/article/details/52180106

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章