Skip to content

Instantly share code, notes, and snippets.

@joeattardi
Created May 28, 2018 01:31
Show Gist options
  • Select an option

  • Save joeattardi/1ab70cde3f0e687629015fc3ade92201 to your computer and use it in GitHub Desktop.

Select an option

Save joeattardi/1ab70cde3f0e687629015fc3ade92201 to your computer and use it in GitHub Desktop.
package com.thinksincode.prboard;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.constraint.Group;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.thinksincode.prboard.github.GitHubApiConstants;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class PostLoginActivity extends AppCompatActivity {
private static final String TAG = PostLoginActivity.class.getSimpleName();
private TextView codeLabel;
private TextView tokenLabel;
private Group resultGroup;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_login);
codeLabel = findViewById(R.id.github_code);
tokenLabel = findViewById(R.id.github_token);
resultGroup = findViewById(R.id.result_group);
progressBar = findViewById(R.id.github_auth_progress_bar);
Intent intent = getIntent();
Uri data = intent.getData();
String code = data.getQueryParameter(GitHubApiConstants.PARAM_CODE);
codeLabel.setText(code);
Uri accessCodeUri = Uri.parse(GitHubApiConstants.URL_TOKEN).buildUpon()
.appendQueryParameter(GitHubApiConstants.PARAM_CLIENT_ID, getString(R.string.github_client_id))
.appendQueryParameter(GitHubApiConstants.PARAM_CLIENT_SECRET, getString(R.string.github_client_secret))
.appendQueryParameter(GitHubApiConstants.PARAM_CODE, code)
.build();
try {
URL accessCodeUrl = new URL(accessCodeUri.toString());
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(accessCodeUrl)
.build();
Log.i(TAG, "Requesting access token from GitHub");
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "Failed to get access token from GitHub: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody responseBody = response.body();
String responseString = responseBody.string();
responseBody.close();
String[] responseData = responseString.split("&");
final String accessToken = responseData[0].split("=")[1];
Log.i(TAG, "Got access token from GitHub successfully");
PostLoginActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
tokenLabel.setText(accessToken);
resultGroup.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
});
}
});
} catch (MalformedURLException mue) {
Log.e(TAG, "Invalid URL: " + accessCodeUri.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment