Xavier Peña
Xavier Peña

Reputation: 7919

How do you get access_token and refresh_token from Imgur?

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.

Upvotes: 5

Views: 7016

Answers (2)

Rub
Rub

Reputation: 2738

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.

Upvotes: 6

Lews Therin
Lews Therin

Reputation: 3777

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

Upvotes: 3

Related Questions