Android Open Source - PrincePolo Request Access Token






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package se.agile.asynctasks;
//from ww  w .jav a  2  s .c om
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import se.agile.model.Preferences;
import android.util.Log;

public class RequestAccessToken extends RequestTask<String, Void, String>{
  private static String OAUTH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
  private final String logTag = "PrincePolo";
  private static boolean isRequestingAccessToken;
  public RequestAccessToken(RequestListener<String> listener){
    super(listener);
  }

  @Override
  protected String doInBackground(String... params) {
    if(!isCancelled()){
      isRequestingAccessToken = true;
      String code = params[0];  
      if(code == null || !code.matches("[\\dA-z]+")){
        Log.e(logTag, "parameter to HttpConnection don't contain 'code'");
        return null;
      }
      HttpClient client = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(OAUTH_ACCESS_TOKEN_URL);
      try {
        JSONObject holder = new JSONObject();
        try {
          holder.put("Accept", "application/json");
          holder.put("client_id", Preferences.getClientId());
          holder.put("client_secret", Preferences.getClientSecret());
          holder.put("code", code);
        } catch (JSONException e1) {
          Log.e(logTag,"Error creating JSON string");
          e1.printStackTrace();
          return null;
        }
        StringEntity se = new StringEntity( holder.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        HttpResponse response = client.execute(httppost);
        HttpEntity respEntity = response.getEntity();
        //          Log.d(logTag, "response Entity Utils: " + EntityUtils.toString(respEntity));
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
          BufferedReader reader = new BufferedReader(new InputStreamReader(respEntity.getContent(), "UTF-8"));
          StringBuilder builder = new StringBuilder();
          String line;
          while ((line = reader.readLine()) != null) {
            builder.append(line);
          }
          return builder.toString();
        }else{
          Log.e(logTag, "Didn't get statuscode 200");
        }
      } catch (ClientProtocolException e) {
        Log.d(logTag, "Error in testBrowser2");
      } catch (IOException e) {
        Log.d(logTag, "Error in testBrowser3");
      }
    }
    return null;
  }
  
  @Override
  protected void onPostExecute(String json){
    isRequestingAccessToken = false;
    if(!isCancelled()){
      String access_token = "";
      if(json != null){
        Log.d(logTag, "json= " + json);
        try {
          JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
          access_token = object.getString("access_token");
          Preferences.setAccessToken(access_token);
          Preferences.setTokenType(object.getString("token_type"));
          Preferences.setScope(object.getString("scope"));
          
        } catch (JSONException e) {
          Log.e(logTag, "Error in interpreting JSON");
          e.printStackTrace();
        }
      }
      finishedWithRequest(access_token);
    }
  }
  
  
  public static boolean isRequestingAccessToken(){
    return isRequestingAccessToken;
  }
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java