0

从 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);
    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();   }
4

2 回答 2

0

问题可能与您使用 HttpClient 有关。看起来 Google 在 Android 6.0 中已经取消了对这个调用的支持。

http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

您应该能够使用 HttpsURLConnection 而不是 Httpclient 来访问 Lifelog Web 服务。

于 2016-01-15T21:41:56.483 回答
0

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

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

}

您用于HTTP_TRANSPORT实例化:

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

于 2016-08-10T19:44:32.720 回答