How do you get access_token and refresh_token from Imgur?
Asked Answered
P

2

5

Project I am using: ImgurNet from nuget (source: https://github.com/0xdeafcafe/ImgurNet)

It seems like it needs all those parameters:

{
    "client_id": "Insert your imgur client_id here",
    "client_secret": "Insert your imgur client_secret here",
    "access_token": "Insert your imgur access_token here", 
    "refresh_token": "Insert your imgur refresh_token here",
    "authorized_username": "Insert your imgur username here"
}

...while in imgur I am only able to get client_id + client_secret.

The Imgur API documentation mentions those, but doesn't say how to get them: https://api.imgur.com/oauth2


Extra details:

I use ImgurNet because it is the only imgur api nuget package that I've been able to install in my Xamarin project (all the other ones were not compatible).

This is an example of the code I'm using:

var oauth2Authentication = new OAuth2Authentication("my_client_id", "my_client_secret", false);
var imgurClient = new Imgur(oauth2Authentication);            
var imageEndpoint = new ImageEndpoint(imgurClient);
var result = imageEndpoint.UploadImageFromBinaryAsync(imageBinary, title: "my title", description: "my description").Result;

And the exception thrown is "Your OAuth AccessToken has expired" (I then refreshed the client_secret with the exact same result).

From the imgur documentation:

If a user has authorized their account but you no longer have a valid access_token for them, then a new one can be generated by using the refresh_token.

...so refresh_token seems necessary regardless.

Pentathlon answered 27/5, 2016 at 14:49 Comment(5)
@Sylverac That's what I did, and I obtained client_id and client_secret, but not access_token and refresh_token.Nuclide
I'm an idiot and misread your question, sorry.Adila
@Sylverac No problem, thanks for trying to help. I'll leave the answer to the comment as clarification to other readers.Nuclide
Although, the instructions for getting an access_token is in the "Authorization" section: "To access a user's account, the user must first authorize your application so that you can get an access token. Requesting an access token is fairly straightforward: point a browser (pop-up, or full page redirect if needed) to a URL and include a set of query string parameters." https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=REQUESTED_RESPONSE_TYPE&state=APPLICATION_STATEAdila
I'm genuinely not trying to be a jerk, but the instructions are all there in the API documentation link you provided. Re-read it carefully and then post specific questions/problems you are having and I'd be happy to help.Adila
A
3

Edit: misunderstood question.

The instructions for getting an access_token is in the "Authorization" section of the API documentation link you provided:

To access a user's account, the user must first authorize your application so that you can get an access token. Requesting an access token is fairly straightforward: point a browser (pop-up, or full page redirect if needed) to a URL and include a set of query string parameters. https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=REQUESTED_RESPONSE_TYPE&state=APPLICATION_STATE

Edit 2:

The API documentation also has a handy table that explains what the parameters are and what possible values you can use:

params

Adila answered 27/5, 2016 at 14:56 Comment(7)
This might be it, but I am having a hard time trying to successfully apply the query. I don't understand the parameters REQUESTED_RESPONSE_TYPE (I have tried json just to try it) and APPLICATION_STATE are unknown to me. And I don't seem to find any documentation about them. The json response in my tests is always {"data":{"error":null,"request":"\/oauth2\/authorize","method":"POST"},"success":false,"status":400} (so "not succeeded").Nuclide
@XavierPeña Once more, read the documentation. Take a second and really read all of it. The answers you are looking for are there. In that same Authorization section, they have a table that outlines what the parameters are and what possible values you can use for them. Ex: "response_type, values: code, token, or pin, Determines if Imgur returns an authorization_code, a PIN code, or an opaque access_token. If you choose code ..."Adila
I've updated my answer to include the table I was referring to. Hope this helps.Adila
Thanks! The final URL that was needed for what I was looking for https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=token. It is a bit confusing since one does not expect the server answer to be in the URL of the page it redirects you to. It took me some seconds to realize that, I thought the query failed again. Thank you very much for your help, I am marking it as answered.Nuclide
@XavierPeña No problem, glad you got it resolved. Happy coding! :)Adila
what would you have for application_string? can you please provide an example?Tack
@LewsTherin I keep getting this error: {"data":{"error":"Imgur is temporarily over capacity. Please try again later."},"success":false,"status":500}Tack
W
6

If you already have the id and secret, just login with your browser on

https://imgur.com/

and then on another tab enter this URL (replace CLIENT_ID)

https://api.imgur.com/oauth2/authorize?client_id=CLIENT_ID&response_type=token

Accept (looks like the image below), and from the resulting URL you need to extract the desired tokens or details.

enter image description here

It is explained in very convoluted ways in many places and it is just that simple.

Some extra info on https://rapidapi.com/blog/imgur-api-tutorial/

Note: This is for any programming language. The Q mentions C# but I used bash & python to connect with Imgur.

Wigley answered 21/4, 2020 at 12:7 Comment(3)
Hi Rub, thanks for your post. I just wonder how can I streamline the re-authorization once the 1st access token is expired. I do not want to receive service down warning and then manually regenerate the new token, since the authorize api endpoint redirects to a webpage and requires human to manually click the Allow button. Is there a better way to do it automatically?Foumart
@Foumart I think that would be better on a new question. My token hasn't expired in years, but it could be different for you.Wigley
Hi Rub, thanks for your reply. I have checked mine, it's going last for years. So I guess it's all good for now. But happy to see the solution if you have a post related to this. CheersFoumart
A
3

Edit: misunderstood question.

The instructions for getting an access_token is in the "Authorization" section of the API documentation link you provided:

To access a user's account, the user must first authorize your application so that you can get an access token. Requesting an access token is fairly straightforward: point a browser (pop-up, or full page redirect if needed) to a URL and include a set of query string parameters. https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=REQUESTED_RESPONSE_TYPE&state=APPLICATION_STATE

Edit 2:

The API documentation also has a handy table that explains what the parameters are and what possible values you can use:

params

Adila answered 27/5, 2016 at 14:56 Comment(7)
This might be it, but I am having a hard time trying to successfully apply the query. I don't understand the parameters REQUESTED_RESPONSE_TYPE (I have tried json just to try it) and APPLICATION_STATE are unknown to me. And I don't seem to find any documentation about them. The json response in my tests is always {"data":{"error":null,"request":"\/oauth2\/authorize","method":"POST"},"success":false,"status":400} (so "not succeeded").Nuclide
@XavierPeña Once more, read the documentation. Take a second and really read all of it. The answers you are looking for are there. In that same Authorization section, they have a table that outlines what the parameters are and what possible values you can use for them. Ex: "response_type, values: code, token, or pin, Determines if Imgur returns an authorization_code, a PIN code, or an opaque access_token. If you choose code ..."Adila
I've updated my answer to include the table I was referring to. Hope this helps.Adila
Thanks! The final URL that was needed for what I was looking for https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=token. It is a bit confusing since one does not expect the server answer to be in the URL of the page it redirects you to. It took me some seconds to realize that, I thought the query failed again. Thank you very much for your help, I am marking it as answered.Nuclide
@XavierPeña No problem, glad you got it resolved. Happy coding! :)Adila
what would you have for application_string? can you please provide an example?Tack
@LewsTherin I keep getting this error: {"data":{"error":"Imgur is temporarily over capacity. Please try again later."},"success":false,"status":500}Tack

© 2022 - 2024 — McMap. All rights reserved.