android - 如何在android中获取访问 token paypal

标签 android android-studio paypal android-volley access-token

我正在使用 paypal SDK在我的项目中,我可以通过我的应用程序付款,但付款后我无法获得access token

这是我的 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);
                    }
                }

这就是我尝试获取访问 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;
        }
    }

我还使用了 volly 和 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);

    }

使用 Volly 它会在我的 logcat 中显示此错误

D/error:﹕ com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: Connection closed by peer

最佳答案

这个对我有用,问题是网络连接安全,所以我换了 wifi,它工作得很好

这就是我收到此错误的原因

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

代码

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
        }

关于android - 如何在android中获取访问 token paypal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910575/

相关文章:

java - OnClickListener 和表格布局

java - java中的NotifyPropertyChanged - Android

java - Android Studio 预览版未显示 ListView 示例

java - 嵌套的 ListViews 没有扩展到全高

php - paypal 批量支付 api 在 php 中的使用

android - 为什么C++可执行文件在Android设备上找不到动态库

java - 为什么 Android 不检索我输入的文本?

java - Google Glass 全屏自定义布局卡

javascript - paypal javascript 按钮 - 如何指定浏览器成功 url?

php - 我的代码有什么问题( future 支付服务器端集成)