How to Upload Transparent Images to Imgur
In Phimpme app we wanted to let a user to upload an image into their personal Imgur account. To accomplish this I take to cosign the user Imgur business relationship in our Phimpme android app and upload the image to their business relationship. In this mail service, I will be explaining how to upload an image on Imgur from Phimpme Android app from user account using authentication token.
Let'southward get started
Step – 1: Register your application on Imgur to receive your Customer ID and Customer Secret.
Register your awarding on Imgur from here and obtain the Customer-Id and Client-Secret. Client-Id volition exist used to authenticate user account.
Pace-two: Authenticate the user
Well, This is the dull job every bit they haven't provided whatever straight API for Java/Android or any other linguistic communication to authenticate. And then, I achieved it via WebView of Android, in Phimpme app nosotros asked user to login into their WebView and the we intercepted network calls and parsed URL to go the admission token once the user logged in.
a.) Load below URL in WebView with your application's client id.
https://api.imgur.com/oauth2/authorize?client_id= ClientId &response_type=token&state=APPLICATION_STATE
WebView imgurWebView = (WebView) findViewById(R.id.LoginWebView); imgurWebView.setBackgroundColor(Color.TRANSPARENT); imgurWebView.loadUrl(IMGUR_LOGIN_URL); imgurWebView.getSettings().setJavaScriptEnabled(truthful); imgurWebView.setWebViewClient(new WebViewClient() { @Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {if (url.contains(REDIRECT_URL)) {splitUrl(url, view); } else { view.loadUrl(url); }render true; } b.) Now, It will ask the user to enter the Imgur username and password after that it volition redirect to the URL, which we added equally a callback URL while adding our app in Imgur dashboard. In Phimpme I added https://org.fossaisa.phimpme as callback URL.
c.)After logging in WebView it will redirect to the callback URL with the following parameters: access_token, refresh_token, authorization_code:
https://api.imgur.com/oauth2/qualify?client_id=YOUR_CLIENT_ID&response_type=REQUESTED_RESPONSE_TYPE&country=APPLICATION_STATE
Step -3
To obtain the admission token from the right URL nosotros have to intercept each and every URL is getting loaded in webview and find the correct URL and parse information technology. For which I accept overridden the shouldOverrideUrlLoading of webViewclient and pass on it to splitUrl method.
private void splitUrl(String url, WebView view) { String[] outerSplit = url.split("\\#")[ane].split("\\&"); Cord username = null; String accessToken = cipher; int alphabetize = 0;for (String south : outerSplit) { Cord[] innerSplit = s.split("\\=");switch (index) { // Access Tokencase 0: accessToken = innerSplit[ane];break; // Refresh Tokencase 3: refreshToken = innerSplit[1];pause; // Usernamecase four: username = innerSplit[1];break;default: } index++; } Now we take user access token of the user to access API to Imgur to do operations(read, write) for that particular user.
Note: Hallmark token is valid for 28 days only, afterward 28 days you lot will take to get the new access token.
Step- 4: Upload image using authentication token
Now we are ready to upload images on Imgur on the behalf of user's account and to upload we need to send requests to Imgur API to this URL with authentication header and body epitome. The trunk volition contain the imageString, title and description.
When we post epitome on Imgur anonymously, we need to add header as Authorization Client-Id {client-id} but to upload image on imgur from an account, we need to add header Authorization Bearer with value equals to access-token and image data in base64 string every bit asking body, as we did in our previous post on upload epitome on Imgur.
Imgur Business relationship Image uploads
That'south it.
The problem I faced:
I take noticed that one token is valid for 28 days only, and then after 28 you will need to ask the user to log in once again in Imgur app.
To avoid this what I did is before 28 days I practise ship a asking to get a new access token which is again valid for next 28 days.
Resource:
- Official Imgur API documentation: https://apidocs.imgur.com/#4b8da0b3-3e73-13f0-d60b-2ff715e8394f
- Upload image Imgur Blog: https://help.imgur.com/hc/en-usa/articles/210076663-Upload-Images
- StackOverflow resources to upload image: https://stackoverflow.com/questions/17352866/imgur-api-uploading
Source: https://blog.fossasia.org/tag/upload-image-on-imgur/
0 Response to "How to Upload Transparent Images to Imgur"
Post a Comment