How to get Access Token paypal in android
Asked Answered
I

1

1

I am using paypal SDK in my project,I am able to make payment from my app,but i am not able to get access token after payment,

This my onActivityResult

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                PaymentConfirmation confirm =
                        data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                if (confirm != null) {
                    try {
                        Log.i(TAG, confirm.toJSONObject().toString(4));
                        Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));


                        /**
                         *  TODO: send 'confirm' (and possibly confirm.getPayment() to your server for verification
                         * or consent completion.
                         * See https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
                         * for more details.
                         *
                         * For sample mobile backend interactions, see
                         * https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend
                         */
                        displayResultText("PaymentConfirmation info received from PayPal");

                        new DownloadLink().execute();


                    } catch (JSONException e) {
                        Log.e(TAG, "an extremely unlikely failure occurred: ", e);
                    }
                }

This is how i trying to get Access Token

 class DownloadLink extends AsyncTask<Void, Void, Void> {


        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub


         HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://api.sandbox.paypal.com/v1/oauth2/token");

            try {
                String text="AUDZWiH7HHihjLqUT3....."+":"+"EDx-t8O2h1.......";
                byte[] data = text.getBytes("UTF-8");
                String base64 = Base64.encodeToString(data, Base64.NO_WRAP);

                httppost.addHeader("content-type", "application/x-www-form-urlencoded");
                httppost.addHeader("Authorization", "Basic " + base64);

                StringEntity se=new StringEntity("grant_type=client_credentials");
                httppost.setEntity(se);

// Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                String responseContent = EntityUtils.toString(response.getEntity());
                Log.d("Response", responseContent );

            } catch (ClientProtocolException e) {
// TODO Auto-generated catch block
            } catch (IOException e) {
// TODO Auto-generated catch block
            }
            //Do Your stuff here..
            return null;
        }
    }

I also used with volly as well as CURL

 public void getaccesstokens()
    {
        String clientID="AUDZWiH7H.........";
        String clientSecret="EDx-t8O2h1.....";
        String text=clientID+":"+clientSecret;
        byte[] data = new byte[0];
        try {
            data = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String base64 = Base64.encodeToString(data, Base64.NO_WRAP);
        StringRequest postRequest=new StringRequest(Request.Method.POST,"https://api.sandbox.paypal.com/v1/oauth2/token",new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.d("accessToken:", response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("error:",volleyError.toString());

                    }
                }){


            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String,String> params=new HashMap<String,String>();
                params.put("grant_type","client_credentials");
                params.put("Authorization", "Basic "+base64);

                return params;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> headers=new HashMap<String,String>();
                headers.put("Accept","application/json");
                headers.put("Accept-Language","en_US");
                headers.put("Content-Type","application/x-www-form-urlencoded");
                return headers;
            }

        };
        postRequest.setRetryPolicy(new

                DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        Volley.newRequestQueue(Testones.this).add(postRequest);

    }

Using Volly it shows this error in my logcat

D/error:﹕ com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: Connection closed by peer
Initiatory answered 12/8, 2016 at 5:28 Comment(9)
see [#6659860 to solve javax.net.ssl.SSLHandshakeException Error?) post probably helpBlent
@ρяσѕρєяK have you done paypal?Initiatory
Aditya problem is not related to PayPal i think, it is related to SSL because POST request URL is "https" instead of "httpBlent
i am using url which mentioned in paypal docInitiatory
Also see devblog.paypal.com/upcoming-security-changes-notice/#sslBlent
@ρяσѕρєяK in IOS my friend is able to get access tokenInitiatory
Because iOS by default support HTTPS protocol but if want to use HTTP protocol in iOS then we need to add Exception in plist.Blent
And is there any issue in trying my suggestion ? just try it once probably fix your issueBlent
Let us continue this discussion in chat.Initiatory
I
0

This one worked for me, The issue was with Network connection security,so i changed my wifi, and it works perfectly fine

That was the reason i was getting this error

NoConnectionError: javax.net.ssl.SSLHandshakeException: Connection closed by peer

CODE

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://api.sandbox.paypal.com/v1/oauth2/token");

        try {
            String text="CLIENT ID"+":"+"SECRET ID";
            byte[] data = text.getBytes("UTF-8");
            String base64 = Base64.encodeToString(data, Base64.NO_WRAP);

            httppost.addHeader("content-type", "application/x-www-form-urlencoded");
            httppost.addHeader("Authorization", "Basic " + base64);

            StringEntity se=new StringEntity("grant_type=client_credentials");
            httppost.setEntity(se);

// Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            String responseContent = EntityUtils.toString(response.getEntity());
            Log.d("Response", responseContent );

        } catch (ClientProtocolException e) {
// TODO Auto-generated catch block
        } catch (IOException e) {
// TODO Auto-generated catch block
        }
Initiatory answered 12/8, 2016 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.