1

I am working on a project where a user can send posts to a Wordpress site. I want to implement this task with the Wordpress rest api v2 plugin.

However, I am unable to Implement oAuth between the Android project and the Wordpress site. Can anyone send example code to create a post method with the Android volley library that has a post param and oAuth on request?

package tmediaa.com.sam02;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    protected static String TAG = "APP_DEBUG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = "http://melketabriz.ir/wp-json/wp/v2/posts/1";

        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest postRequest = new StringRequest(Request.Method.DELETE, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        // response
                        Log.d("TAG", response);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("TAG","error => "+error.toString());
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                String key = "Authorization";
                String encodedString = Base64.encodeToString(String.format("%s:%s", "user_site_tmediaaAdmin", "8LO8EnfFb%7h94LE").getBytes(), Base64.NO_WRAP);
                String value = String.format("Basic %s", encodedString);
                map.put(key, value);

                return map;
            }
        };
        queue.add(postRequest);
    }


}
when i run this code, i got a message like this: D/TAG: error => com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found
Vahid Arjmand
  • 73
  • 1
  • 8
  • To send OAuth, I think you can override `getHeaders()` as in http://stackoverflow.com/questions/34310134/send-authentication-information-with-volley-request/34322783#34322783, of course replace with your credentials (key, secret...) (or `getParams()` if `getHeaders()` does not work), Regarding POST params, just need to use `JsonObjectRequest` or `JsonArrayRequest` with `JSONObject jsonRequest` not null – BNK Nov 19 '16 at 04:08
  • when i use this technique, i got problem with this message : No OAuth parameters supplied, – Vahid Arjmand Nov 19 '16 at 09:47
  • You should post your code and the URL of Web Service – BNK Nov 19 '16 at 10:23
  • @BNK edit answare and add project code – Vahid Arjmand Nov 19 '16 at 12:09
  • Your code does not send Oauth credentials . It sends Basic Authentication credentials instead, I think you should read http://v2.wp-api.org/guide/authentication/ first – BNK Nov 19 '16 at 15:05
  • how to send Oauth credentials with Volley ? is there any sample code? – Vahid Arjmand Nov 19 '16 at 17:03
  • I don't have, pls search in SO – BNK Nov 20 '16 at 09:59

0 Answers0