x13thangelx

auth

Dec 10th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package com.dazzleapp.dazzle;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.os.Looper;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.io.*;
  21. import java.net.HttpURLConnection;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.net.URLEncoder;
  25. import java.util.Scanner;
  26.  
  27. import static android.widget.Toast.LENGTH_SHORT;
  28. import static android.widget.Toast.makeText;
  29.  
  30.  
  31. @SuppressLint({ "NewApi", "NewApi" })
  32. public class Auth extends Activity {
  33.  
  34.     public String usrname, uname, pw, pass, param, response;
  35.    
  36.     @Override
  37.     public void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.auth);
  40.     }
  41.    
  42.    
  43.     public void auth(View v) throws JSONException {
  44.          EditText username = (EditText) findViewById(R.id.usernameInput);
  45.          final String usrname = username.getText().toString();
  46.          final String uname = "&username=" + usrname;
  47.    
  48.          EditText upw = (EditText) findViewById(R.id.passwordInput);
  49.          final String pw = upw.getText().toString();
  50.          final String pass = "&password=" + pw;
  51.          
  52.          if (upw.length()>0 || username.length()>0){
  53.              class Data extends AsyncTask<String, Void, String>{
  54.                  protected String doInBackground(String... urls){
  55.                      Looper.prepare();
  56.                      URL url;
  57.                      HttpURLConnection conn;
  58.  
  59.                      InputStream is = null;
  60.                      String result = "";
  61.                      JSONObject jArray = null;
  62.  
  63.                      //post
  64.                      try{
  65.                          url = new URL("https://alpha.app.net/oauth/access_token");
  66.                          String param = "client_id=" + URLEncoder.encode("<client_id>","UTF-8")+
  67.                                  "&password_grant_secret=" + URLEncoder.encode("<pw_grant>", "UTF-8") +
  68.                                  "&grant_type=" + URLEncoder.encode("password", "UTF-8") +
  69.                                  "&username=" + URLEncoder.encode(usrname, "UTF-8") +
  70.                                  "&password=" + URLEncoder.encode(pw, "UTF-8") +
  71.                                  "&scope=" + URLEncoder.encode("basic,stream,email,write_post,follow,messages", "UTF-8");
  72.  
  73.                          conn = (HttpURLConnection)url.openConnection();
  74.                          conn.setDoOutput(true);
  75.                          conn.setDoInput(true);
  76.                          conn.setRequestMethod("POST");
  77.  
  78.                          conn.setFixedLengthStreamingMode(param.getBytes().length);
  79.                          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  80.  
  81.                          PrintWriter out = new PrintWriter(conn.getOutputStream());
  82.                          out.print(param);
  83.                          out.close();
  84.  
  85.                          BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()),8096);
  86.                          StringBuilder sb = new StringBuilder();
  87.                          String line = null;
  88.                          while ((line = reader.readLine()) != null){
  89.                              sb.append(line + "\n");
  90.                          }
  91.  
  92.                          is.close();
  93.                          result = sb.toString();
  94.  
  95.                      /*} catch(Exception e){
  96.                          Log.e("log_tag", "Error in http connection " + e.toString());
  97.                      }
  98.  
  99.                      //convert response
  100.                      try{
  101.                          BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()),8096);
  102.                          StringBuilder sb = new StringBuilder();
  103.                          String line = null;
  104.                          while ((line = reader.readLine()) != null){
  105.                              sb.append(line + "\n");
  106.                          }
  107.  
  108.                          is.close();
  109.                          result = sb.toString();   */
  110.                      }catch(Exception e){
  111.                          Log.e("log_tag", "Error converting result " + e.toString());
  112.                      }
  113.  
  114.                      //parse response to JSON object
  115.                      try{
  116.                          jArray = new JSONObject(result);
  117.                      }catch (JSONException e){
  118.                          Log.e("log_tag", "Error parsing data " + e.toString());
  119.                      }
  120.                  return null;
  121.                  }
  122.              }
  123.              Data authTask = new Data();
  124.              authTask.execute();
  125.  
  126.          } else {
  127.              makeText(getBaseContext(), "All field are required", LENGTH_SHORT).show();
  128.          }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment