Created
August 9, 2017 02:53
-
-
Save vudunguit/3d637fb77c21146ef75e31d6bfc87736 to your computer and use it in GitHub Desktop.
Instagram accesstoken (sample)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InstaModel { | |
public static final String CLIENT_ID = "31f1cc932c264e5da2711ad437753c78"; | |
public static final String CLIENT_SECRET = "3db8f5ff74474cadba35a6a2bf518b5d"; | |
public static final String CALLBACK_URL = "https://medium.com/@vutiendunguit"; | |
public static final String GRANT_TYPE = "authorization_code"; | |
public static final String AUTH_URL = "https://api.instagram.com/oauth/authorize/"; | |
public static final String TOKEN_URL = "https://api.instagram.com/oauth/access_token"; | |
public static final String API_URL = "https://api.instagram.com/v1"; | |
private static final String TAG = InstaModel.class.getSimpleName(); | |
private AppPreference appPreference; | |
private InstaService instaService; | |
public InstaModel(AppPreference appPreference, InstaService instaService) { | |
this.appPreference = appPreference; | |
this.instaService = instaService; | |
} | |
public interface ResponseListener<T>{ | |
void onSuccess(T data); | |
void onError(String message); | |
} | |
public Observable<AccessToken> getAccessToken(String code){ | |
return instaService.getAccessToken(CLIENT_ID, CLIENT_SECRET, GRANT_TYPE, CALLBACK_URL, code) | |
.doOnNext(accessToken -> appPreference.storeAccessToken(accessToken)); | |
} | |
public Observable<List<UserDetail>> findUsersFromLocation(LatLng latLng){ | |
final String accessToken = appPreference.getAccessToken(); | |
return instaService.searchMedia(10.815157, 106.695171 /*latLng.latitude, latLng.longitude*/, accessToken, 5) | |
.flatMap((Media media) -> instaService.getUserDetail(media.user.getId(), accessToken)) | |
.toList() | |
.toObservable(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginFragment extends DaggerFragment<MainComponent> { | |
@BindView(R.id.webview) WebView webview; | |
@BindView(R.id.loading) ProgressBar loading; | |
private String mAuthUrl; | |
private String mTokenUrl; | |
@Inject InstaModel instaModel; | |
private Callback callback; | |
private final String TAG = LoginFragment.class.getSimpleName(); | |
@Override protected int layoutResId() { | |
return R.layout.fragment_login; | |
} | |
@Override public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
daggerComponent().inject(this); | |
mTokenUrl = InstaModel.TOKEN_URL + "?client_id=" + InstaModel.CLIENT_ID + "&client_secret=" | |
+ InstaModel.CLIENT_SECRET + "&redirect_uri=" + InstaModel.CALLBACK_URL + "&grant_type=authorization_code"; | |
mAuthUrl = InstaModel.AUTH_URL + "?client_id=" + InstaModel.CLIENT_ID + "&redirect_uri=" | |
+ InstaModel.CALLBACK_URL + "&response_type=code&display=touch&scope=public_content+likes+comments+relationships"; | |
} | |
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
callback = (Callback)getActivity(); | |
CookieManager cookieManager = CookieManager.getInstance(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
Log.i(TAG, "REMOVING COOKIES"); | |
cookieManager.removeAllCookies(null); | |
CookieManager.getInstance().flush(); | |
}else{ | |
cookieManager.removeAllCookie(); | |
} | |
webview.setVerticalScrollBarEnabled(false); | |
webview.setHorizontalScrollBarEnabled(false); | |
webview.setWebViewClient(new OAuthWebViewClient()); | |
webview.getSettings().setJavaScriptEnabled(true); | |
webview.loadUrl(mAuthUrl); | |
} | |
private class OAuthWebViewClient extends WebViewClient { | |
@Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { | |
Log.d(TAG, "Redirecting URL " + view.getUrl()); | |
return super.shouldOverrideUrlLoading(view, request); | |
} | |
@Override | |
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | |
super.onReceivedError(view, request, error); | |
Log.d(TAG, "Page error: "); | |
showAlertError("Received error"); | |
} | |
@Override | |
public void onReceivedError(WebView view, int errorCode, | |
String description, String failingUrl) { | |
Log.d(TAG, "Page error: " + description); | |
super.onReceivedError(view, errorCode, description, failingUrl); | |
showAlertError(description); | |
} | |
@Override | |
public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
Log.d(TAG, "Loading URL: " + url); | |
super.onPageStarted(view, url, favicon); | |
if (url.startsWith(InstaModel.CALLBACK_URL)) { | |
String urls[] = url.split("="); | |
getAccessTokenFromCode(urls[1]); | |
} | |
showLoading(true); | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
super.onPageFinished(view, url); | |
Log.d(TAG, "onPageFinished URL: " + url); | |
showLoading(false); | |
} | |
} | |
private void showAlertError(String error){ | |
new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogCustom)) | |
.setTitle("Error") | |
.setMessage(error) | |
.setPositiveButton("OK", null) | |
.create() | |
.show(); | |
} | |
private void getAccessTokenFromCode(String code){ | |
showLoading(true); | |
instaModel.getAccessToken(code) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(accessToken -> { | |
Log.i(TAG,"Response: " + accessToken.access_token); | |
callback.onSuccess(); | |
}, throwable -> { | |
throwable.printStackTrace(); | |
showAlertError(throwable.getMessage());}); | |
Log.e(TAG, "Code: " + code); | |
} | |
private void showLoading(boolean show){ | |
if(loading != null) { | |
loading.setVisibility(show ? View.VISIBLE : View.GONE); | |
} | |
} | |
public interface Callback{ | |
void onSuccess(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment