Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected String doInBackground(Context... contexts) {
- // get the string from params, which is an array
- Context context = contexts[0];
- System.out.println("Got context: " + context.toString());
- // Do something that takes a long time, for example:
- OkHttpClient client = new OkHttpClient();
- /* if (BuildConfig.DEBUG) {
- client.interceptors().add(new LoggingInterceptor());
- } */
- System.out.println("Created new OkHttpClient");
- try {
- InputStream inputStream = context.getResources().openRawResource(R.raw.keys);
- String jsonString = new Scanner(inputStream).useDelimiter("\\A").next();
- JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
- String CLIENT_ID = object.optString("client_id");
- String REDIRECT_URI = object.optString("redirect_uri");
- String CLIENT_SECRET = object.optString("client_secret");
- String AUTHORIZATION_CODE = object.optString("authorization_code");
- String GRANT_TYPE = object.optString("grant_type");
- String TARGET_URL = object.optString("token_target_url");
- System.out.println("Got everything from keys.json");
- RequestBody bodyBuilder = new FormBody.Builder()
- .addEncoded("client_secret", CLIENT_SECRET)
- .addEncoded("client_id", CLIENT_ID)
- .add("code", AUTHORIZATION_CODE)
- .add("grant_type", GRANT_TYPE)
- .add("redirect_uri", REDIRECT_URI)
- .build();
- System.out.println("Built body: " + bodyBuilder.toString());
- // String mediaTypeString = "application/x-www-form-urlencoded";
- // MediaType mediaType = MediaType.parse(mediaTypeString);
- // RequestBody body = RequestBody.create(mediaType, bodyBuilder.toString());
- Request request = new Request.Builder()
- .url(TARGET_URL)
- .post(bodyBuilder)
- // .addHeader("content-type", mediaTypeString)
- .addHeader("cache-control", "no-cache")
- .build();
- try {
- System.out.println("Starting request.");
- Response response = client.newCall(request).execute();
- String targetUrl = request.url().toString() + bodyToString(request);
- System.out.println("request: " + targetUrl);
- String responseBodyString = response.body().string();
- System.out.println("response: " + responseBodyString);
- return responseBodyString;
- } catch (IOException ex) {
- System.out.println("Oof. IOException - take a look! " + ex);
- }
- return "Error in getting access token.";
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return "Error in getting keys.json";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement