I need to integrate Reddit to android app. I don't need much. Only log in and post a new link. (something like a share on Facebook).
But I am struggling on the very beginning.
Any help will b appreciated.
EDIT: Here is what I made so far, I get the modhash and captcha but when I try to submit new link it gives me this: {"json": {"errors": [["USER_REQUIRED", "please login to do that", null]]}}
private void getModhash(){
modhash = dbHandler.getUserModahash();
if(modhash.equals("")){
String jsonString = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(3);
fields.add(new BasicNameValuePair("user", "my_username"));//will ask for a user to enter the password later
fields.add(new BasicNameValuePair("passwd", "my_password"));
fields.add(new BasicNameValuePair("api_type", "json"));
final HttpPost request = new HttpPost("https://ssl.reddit.com/api/login");
try {
request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
jsonString = EntityUtils.toString(entity);
System.out.println("response from redit = " + jsonString);
JSONObject jObject = new JSONObject(jsonString);
modhash = jObject.getJSONObject("json").getJSONObject("data").getString("modhash");
dbHandler.addRedditModahash(modhash);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void getCaptcha(){
String jsonString = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(1);
fields.add(new BasicNameValuePair("api_type", "json"));
final HttpPost request = new HttpPost("https://ssl.reddit.com/api/new_captcha");
try {
request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
jsonString = EntityUtils.toString(entity);
System.out.println("CAPTCHA response = " + jsonString);
JSONObject jObject = new JSONObject(jsonString);
iden = jObject.getJSONObject("json").getJSONObject("data").getString("iden");
System.out.println("IDEN = " + iden);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
showCaptcha();
}
private void showCaptcha(){
// custom dialog
final Dialog dialog = new Dialog(Share.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_img_edt);
try {
URL url = new URL("https://ssl.reddit.com/captcha/" + iden);
final Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
ImageView img = (ImageView) dialog.findViewById(R.id.img_captcha);
img.setImageBitmap(bmp);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Button btn_dialog_submit = (Button) dialog.findViewById(R.id.btn_captcha);
final EditText edt_dialog = (EditText) dialog.findViewById(R.id.edt_answer);
btn_dialog_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String answ = edt_dialog.getText().toString();
if(answ.length() > 0){
answerToCaptcha = answ;
postToReddit();
dialog.dismiss();
}
else
Toast.makeText(getApplicationContext(), "Enter answer to captcha..", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
private void postToReddit(){
System.out.println("Captcha iden = " + iden);
System.out.println("Captcha answer = " + answerToCaptcha);
String jsonString = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(14);
fields.add(new BasicNameValuePair("user", "my_username"));
fields.add(new BasicNameValuePair("passwd", "my_password"));//just thought that this would solve the problem but it doesn't
fields.add(new BasicNameValuePair("api_type", "json"));
fields.add(new BasicNameValuePair("captcha", answerToCaptcha));
fields.add(new BasicNameValuePair("extension", "json"));
fields.add(new BasicNameValuePair("iden", "json"));
fields.add(new BasicNameValuePair("kind", iden));
fields.add(new BasicNameValuePair("resubmit", "true"));
fields.add(new BasicNameValuePair("save", "true"));
fields.add(new BasicNameValuePair("sendreplies", "true"));
fields.add(new BasicNameValuePair("sr", "Money"));
fields.add(new BasicNameValuePair("text", "We will beat ANY qoute from ANY Bank! We will transfer your money FEE FREE!"));
fields.add(new BasicNameValuePair("then", "comments"));
fields.add(new BasicNameValuePair("title", "Check this cool app"));
fields.add(new BasicNameValuePair("url", "http://www.the_link_to_app.com/"));
final HttpPost request = new HttpPost("https://ssl.reddit.com/api/submit");
request.addHeader("X-Modhash", modhash);
try {
request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
jsonString = EntityUtils.toString(entity);
System.out.println("response from redit = " + jsonString);
JSONObject jObject = new JSONObject(jsonString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
I also tried to add Cookie, but that didn't helped:
CookieStore cookieStore = new BasicCookieStore();
Cookie cookiee = new BasicClientCookie("Cookie", cookie);
cookieStore.addCookie(cookiee);
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
Where string cookie is retrieved from login. and when I execute the it:
HttpResponse response = httpclient.execute(request, localContext);
But that didn't helped.. same error message.
Any ideas where is the mistake? Or I try to do that in a wrong way? Any help please!
Ok, so I found a solution for this (maybe not a perfect one but it works).
So first of all download the raw4j reddit api wrapper https://github.com/corydissinger/raw4j
The submit a new link should be included pretty soon. But if you will download it and the submit function will not be pressent (I posted my changes to the developer of raw4j but don't know when he will update it) add these:
To "RedditApiParameterConstants.java" add these:
And to the "Reddit.java" add this:
}
And then just build the *.jar file (if it was not included) from that project and add it to your project.
Hope this will save some time for other developers.