· 6 years ago · Feb 07, 2020, 05:10 PM
1package com.example.epicture;
2import com.example.epicture.Operability;
3import androidx.appcompat.app.AlertDialog;
4import androidx.appcompat.app.AppCompatActivity;
5
6import android.content.Context;
7import android.content.Intent;
8import android.graphics.Bitmap;
9
10import android.net.ConnectivityManager;
11import android.net.NetworkInfo;
12import android.os.AsyncTask;
13import android.os.Bundle;
14import android.util.Log;
15import android.view.View;
16import android.widget.Button;
17import android.widget.EditText;
18import android.widget.TextView;
19import android.webkit.*;
20
21import org.json.JSONArray;
22import org.json.JSONException;
23import org.json.JSONObject;
24import org.w3c.dom.Text;
25
26import java.io.IOException;
27import java.net.MalformedURLException;
28import java.net.URL;
29import java.util.ArrayList;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
35
36import okhttp3.Call;
37import okhttp3.Callback;
38import okhttp3.FormBody;
39import okhttp3.MediaType;
40import okhttp3.MultipartBody;
41import okhttp3.OkHttpClient;
42import okhttp3.Request;
43import okhttp3.RequestBody;
44import okhttp3.Response;
45import okhttp3.ResponseBody;
46
47
48public class MainActivity extends AppCompatActivity {
49
50 protected Request request;
51 private WebView mLoginWebView;
52 private URL loginURL;
53 private URL newUrl;
54 private String _Query;
55 private String[] _Query_List;
56 private String[] to_Hash;
57 private Map<String, String> tokens;
58
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 //Request __request = Spliter("access_token");
64 // _API(__request);
65 CookieManager.getInstance().removeAllCookies(null);
66 _loginImgur();
67 Request _request = Spliter("postImages");
68 _API(_request);
69 }
70
71
72 protected Request getAccount() {
73
74 Request request = new Request.Builder()
75 .url("https://api.imgur.com/3/account/Meznev31")
76 .method("GET", null)
77 .addHeader("Authorization", "Client-ID d116b132e74a712")
78 .build();
79
80 return request;
81 }
82
83
84 protected Request getImages() {
85
86 Request request = new Request.Builder()
87 .url("https://api.imgur.com/3/account/me/images")
88 .method("GET", null)
89 .addHeader("Authorization", "Bearer 9a7cdb601d955daef3dd8b6ce8ad5bf3a1e834c7")
90 .build();
91 return request;
92 }
93
94 protected Request getGalleryImages() {
95
96 Request request = new Request.Builder()
97 .url("https://api.imgur.com/3/account/Meznev31/settings")
98 .method("GET", null)
99 .addHeader("Authorization", "Bearer 9a7cdb601d955daef3dd8b6ce8ad5bf3a1e834c7")
100 .build();
101 return request;
102 }
103
104 protected Request postImage() {
105 RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
106 .addFormDataPart("image", "https://s3-eu-west-1.amazonaws.com/images-ca-1-0-1-eu/recipe_photos/slide/344/epis-de-mais-3000x2000.jpg")
107 //.addFormDataPart("type" , "URL")
108 .build();
109 Request request = new Request.Builder()
110 .url("https://api.imgur.com/3/upload")
111 .method("POST", body)
112 //.addHeader("Authorization", "Client-ID d116b132e74a712")
113 .addHeader("Authorization", "Bearer 9a7cdb601d955daef3dd8b6ce8ad5bf3a1e834c7")
114 .build();
115
116 return request;
117 }
118
119 protected void _loginImgur() {
120 setContentView(R.layout.activity_main);
121 try {
122 loginURL = new URL("https://api.imgur.com/oauth2/authorize?client_id=d116b132e74a712&response_type=token");
123 } catch (MalformedURLException syntax) { syntax.printStackTrace(); }
124
125
126 mLoginWebView = findViewById(R.id.loginWebView);
127 mLoginWebView.loadUrl(loginURL.toString());
128 Log.v("caca", mLoginWebView.getUrl());
129 mLoginWebView.setWebViewClient(new WebViewClient(){
130 @Override
131 public void onPageStarted(WebView view, String url, Bitmap favicon) {
132 super.onPageStarted(view, url, favicon);
133 String url_pute = url.replace("#", "?");
134 url_pute = url_pute.replace("bearer", "Bearer");
135 Log.v("Url", url);
136 try {
137 newUrl = new URL(url_pute);
138 _Query = newUrl.getQuery();
139 } catch (MalformedURLException syntax) { syntax.printStackTrace();}
140
141 try {
142 Log.v("cacaba", newUrl.toString());
143 Log.v("cacaba", "Query : "+ _Query);
144 } catch (NullPointerException e) {
145 e.printStackTrace();
146 }
147
148 if (!loginURL.toString().equals(url)) {
149 _QueryList(_Query);
150 Intent HomePage = new Intent(MainActivity.this, Accueil.class);
151 startActivity(HomePage);
152 }
153 }
154 });
155 }
156
157 protected void _QueryList(String query) {
158 _Query_List = query.split("&");
159 if (_Query_List.length > 1) {
160
161 Log.v("tokens ", "Taille _Query_List : " + String.valueOf(_Query_List.length));
162 Log.v("tokens ", "_Query_List : " + _Query_List[0]);
163 Log.v("tokens ", "_Query_List : " + _Query_List[1]);
164 Log.v("tokens ", "_Query_List : " + _Query_List[2]);
165 Log.v("tokens ", "_Query_List : " + _Query_List[3]);
166 Log.v("tokens ", "_Query_List : " + _Query_List[4]);
167 Log.v("tokens ", "_Query_List : " + _Query_List[5]);
168 tokens = new HashMap<>();
169 for (int i = 0; i < _Query_List.length; i++) {
170 to_Hash = null;
171 to_Hash = _Query_List[i].split("=");
172 Log.v("tokens ", to_Hash[0]);
173 Log.v("tokens ", to_Hash[1]);
174 tokens.put(to_Hash[0], to_Hash[1]);
175 }
176 }
177 for (Map.Entry<String,String> entry : tokens.entrySet()) {
178 Log.v("tokens ", "Key = " + entry.getKey() +
179 ", Value = " + entry.getValue());
180 }
181 Log.v("tokens ", tokens.get("access_token"));
182
183}
184
185 protected Request getAccessToken() {
186
187
188 MediaType mediaType = MediaType.parse("text/plain");
189 RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
190 .addFormDataPart("refresh_token", "1b4565f1db09d8b046286e5f731028297291ae92")
191 .addFormDataPart("client_id", "7f3e38d735e7fbb")
192 .addFormDataPart("client_secret", "52dd92791da04890a3e3e6fa421eef4a64d63d08")
193 .addFormDataPart("grant_type", "refresh_token")
194 .build();
195 Request request = new Request.Builder()
196 .url("https://api.imgur.com/oauth2/token")
197 .method("POST", body)
198 .build();
199
200 return request;
201
202 }
203
204 protected Request Spliter(String _type) {
205
206 Log.v("Toya" , "Spliter");
207 if (_type == "access_token")
208 return getAccessToken();
209 if (_type == "getAccount")
210 return getAccount();
211 if (_type == "getImages")
212 return getImages();
213 if (_type == "getGalleryImages")
214 return getGalleryImages();
215 if (_type == "postImages")
216 return postImage();
217
218
219 return this.request;
220 }
221
222 protected void _API(Request request) {
223 Log.v("Toya" , "_API");
224 try {
225 OkHttpClient client = new OkHttpClient().newBuilder()
226 .build();
227 CallbackFuture future = new CallbackFuture();
228 client.newCall(request).enqueue(future);
229 try {
230 Response response = future.get();
231 Log.v("TEST1" , response.toString());
232 printJson(response);
233 } catch(ExecutionException e) {
234 e.printStackTrace();
235 }
236 } catch (InterruptedException e) {
237 e.printStackTrace();
238 }
239 }
240
241 public void printJson(Response response) {
242 try
243 {
244 String json = response.body().string();
245 JSONObject jsonObject = new JSONObject(json);
246 JSONArray jsonData = (JSONArray) jsonObject.getJSONArray("data");
247 String responseString = jsonData.toString();
248 Log.v("TEST33", jsonData.toString()); //RETURNS JSON :D
249 Log.v("TEST1", responseString); //RETURNS JSON :D
250 } catch (IOException | JSONException e) {
251 e.printStackTrace();
252 }
253 }
254}
255
256class CallbackFuture extends CompletableFuture<Response> implements Callback {
257 public void onResponse(Call call, Response response) {
258 super.complete(response);
259 }
260 public void onFailure(Call call, IOException e){
261 super.completeExceptionally(e);
262 }
263}