javax.net.ssl.sslpeerunverifiedexception lifelogApi 中没有对等证书错误 [英] javax.net.ssl.sslpeerunverifiedexception no peer certificate Error In lifelogApi

查看:41
本文介绍了javax.net.ssl.sslpeerunverifiedexception lifelogApi 中没有对等证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在从 Lifelog api 获取访问令牌时收到 SSL peer unverified 错误.我能够获取验证码,但是当我尝试获取访问令牌时,它给了我 SSL 对等错误.它在少数设备上运行良好,但大多数设备都出现 SSL 错误.

private void getAccessToken(final String authCode){final String finalUrl = String.format("https://platform.lifelog.sonymobile.com/oauth/2/token?client_id=%s&client_secret=%s&code=%s",CLIENT_ID,CLIENT_SECRET,authCode);线程网络线程 = 新线程(新可运行(){公共无效运行(){尝试 {HttpClient 客户端 = 新的 DefaultHttpClient();HttpPost post = new HttpPost(finalUrl);//添加你的数据ArrayListnameValuePairs = new ArrayList(4);nameValuePairs.add(new BasicNameValuePair("client_id", CLIENT_ID));nameValuePairs.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));nameValuePairs.add(new BasicNameValuePair("code", authCode));AbstractHttpEntity ent=new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");post.setEntity(ent);//执行 HTTP Post 请求HttpResponse 响应 =null;尝试 {response = client.execute(post);Log.d("响应:" , response.toString());} catch (IOException e) {e.printStackTrace();}String dataObject = response.toString();JSONObject 对象;如果(数据对象!= null){对象 = 空;尝试 {String json_string = EntityUtils.toString(response.getEntity());//displayToast(json_string);obj = 新的 JSONObject(json_string);SharedPreferences prefs =getSharedPreferences("Myprefs", Context.MODE_PRIVATE);prefs.edit().putString("Access_token", obj.getString("access_token"));//prefs.edit().putString(AUTH_REFRESH_TOKEN, obj.getString(AUTH_REFRESH_TOKEN));} catch (JSONException e) {e.printStackTrace();}}} catch (IOException e) {//TODO 自动生成的 catch 块}}});networkThread.start();}

解决方案

我正在使用 google-oauth-client,我能够在 Android 5.x 上使用这个初始化>

import com.google.api.client.http.HttpTransport;

private void initializeSocketFactory() {如果(Build.VERSION.SDK_INT >= 23){HTTP_TRANSPORT = new NetHttpTransport();} 别的 {//Android 5 及以下版本需要此 SSL Socket 工厂初始化尝试 {SSLContext sslContext = SSLContext.getInstance("TLSv1");sslContext.init(null, null, null);SSLSocketFactory socketFactory = sslContext.getSocketFactory();NetHttpTransport.Builder netTransportBuilder = new NetHttpTransport.Builder();netTransportBuilder.setSslSocketFactory(socketFactory);HTTP_TRANSPORT = netTransportBuilder.build();} catch (NoSuchAlgorithmException | KeyManagementException e) {Log.e(LOG_TAG, "ssl 套接字实例化密码问题", e);}}}

您使用 HTTP_TRANSPORT 来实例化:

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;

We are getting SSL peer unverified error while fetching the access token from Lifelog api. I am able to get the authcode, but when i am trying to get access token, it is giving me SSL peer error. It works fine with few device, but most of the device it is giving SSL error.

private void getAccessToken(final String authCode)
{
final String finalUrl = String.format("https://platform.lifelog.sonymobile.com/oauth/2/token?client_id=%s&client_secret=%s&code=%s",CLIENT_ID,CLIENT_SECRET,authCode);
    Thread networkThread = new Thread(new Runnable() {
        public void run() {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(finalUrl);
                // Add your data
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                nameValuePairs.add(new BasicNameValuePair("client_id", CLIENT_ID));
                nameValuePairs.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
                nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
                nameValuePairs.add(new BasicNameValuePair("code", authCode));
                AbstractHttpEntity ent=new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
                ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
                post.setEntity(ent);
                // Execute HTTP Post Request
                HttpResponse response =null;
                try {
                    response = client.execute(post);
                    Log.d("Response:" , response.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String dataObject =  response.toString();
                JSONObject obj;
                if(dataObject != null) {
                    obj = null;

                    try {

                        String json_string = EntityUtils.toString(response.getEntity());
                        //     displayToast(json_string);
                        obj = new JSONObject(json_string);
                        SharedPreferences prefs =getSharedPreferences("Myprefs", Context.MODE_PRIVATE);

                        prefs.edit().putString("Access_token", obj.getString("access_token"));

//                            prefs.edit().putString(AUTH_REFRESH_TOKEN, obj.getString(AUTH_REFRESH_TOKEN));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }  catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }
    });
    networkThread.start();   }

解决方案

I'm using google-oauth-client, I was able to use on Android 5.x with this initialization for

import com.google.api.client.http.HttpTransport;

private void initializeSocketFactory() {
    if (Build.VERSION.SDK_INT >= 23) {
        HTTP_TRANSPORT = new NetHttpTransport();
    } else {
        //Android 5 and bellow needs this SSL Socket factory initialization
        try {
            SSLContext sslContext = SSLContext.getInstance("TLSv1");
            sslContext.init(null, null, null);
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            NetHttpTransport.Builder netTransportBuilder = new NetHttpTransport.Builder();
            netTransportBuilder.setSslSocketFactory(socketFactory);
            HTTP_TRANSPORT = netTransportBuilder.build();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            Log.e(LOG_TAG, "Problem instantiating cipher for ssl socket", e);
        }
    }

}

You use HTTP_TRANSPORT to instantiate:

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;

这篇关于javax.net.ssl.sslpeerunverifiedexception lifelogApi 中没有对等证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆