1

im trying to upload to my imgur Account's album, however, the upload works, but it looks like its gonna uploaded as anonym/public?

here's my code:

<?php

$client_id = "465xxx8c44294";
$image = file_get_contents("../images/d4487317c3xxx93210b293c2e.jpg");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
//curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image), 'album' => 'nqxxxGE'));

$reply = curl_exec($ch);
curl_close($ch);

$reply = json_decode($reply);
printf('<img height="180" src="%s" >', $reply->data->link);

any advice?

  • I want to have this picture placed in my AlbumID "nqxxxGE" on my Imgur account. –  Oct 01 '19 at 12:29

1 Answers1

0

If you want to add image to album, according doc, you need to pass album id. Make sure you've generated token that has access to secret albums. Here you can find some tips about tokens.

curl_setopt($ch, CURLOPT_POSTFIELDS, 
    array(
        'image' => base64_encode($image), 
        'album' => '5' // 5 - your album id
    ) 
); 

You can check you albums id using this api.

To refresh token:

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.

To obtain a new access token, your application performs a POST to https://api.imgur.com/oauth2/token. The request must include the following parameters to use a refresh token:

refresh_token: The refresh token returned from the authorization code exchange

client_id: The client_id obtained during application registration

client_secret: The client secret obtained during application registration.

grant_type: As defined in the OAuth2 specification, this field must contain a value of: refresh_token.

As long as the user has not revoked the access granted to your application, the response includes a new access token. A response from such a request is shown below:

{
    "access_token":"5c3118ebb73fbb275945ab340be60b610a3216d6",
    "refresh_token":"d36b474c95bb9ee54b992c7c34fffc2cc343d0a7",
    "expires_in":3600,
    "token_type":"Bearer",
    "account_username":"saponifi3d"
}

Add the refresh part at the start of your script. Something like:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/oauth2/token');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'refresh_token' => $refreshToken, // Your refresh_token
    'client_id' => $client_id,
    'client_secret' => $clientSecret, //Your client_secret
    'grant_type' =>  'refresh_token'
]);

//Keep in mind that refreshToken and clientSecret are obtained during registration.

$reply = curl_exec($ch);
curl_close($ch);

$reply = json_decode($reply);
$accessToken = $reply->access_token;
freeek
  • 985
  • 7
  • 22
  • yeah, i did that, but its just not adding it. is that the public ID? or i do not have any clue how to get the "ID" of the album o_O –  Oct 01 '19 at 12:58
  • @Thomas should be public. Check using api in the updated answer :) – freeek Oct 01 '19 at 13:06
  • Okay, so i can't upload to hidden albums? –  Oct 01 '19 at 13:15
  • @Thomas I am not sure, just try to get all your album ids. I provided you the [link](https://apidocs.imgur.com/?version=latest#c13fe966-0f8f-425d-9247-298e302e1bc0) howto do this. I think, there are also hidden albums, so should be possible to use it by id. – freeek Oct 01 '19 at 13:17
  • @Thomas check your cliend id has not expired, for me it works fine using secret albums. – freeek Oct 01 '19 at 13:53
  • lol, it was because i just created a "public" token, which had no access to my album. Now, i have a access_token. but that expires after 28 days :/ –  Oct 01 '19 at 14:53
  • @Thomas you can [refresh](https://apidocs.imgur.com/?version=latest#3f80c836-8f49-4fb1-95a7-a4b058265d72) it :) – freeek Oct 01 '19 at 14:54
  • Yeah saw that! But i need to manually do it every 28 days? :( –  Oct 01 '19 at 14:59
  • @Thomas I've edited my answer to provide you some usefull link about tokens. If you pass `refresh_token` you can generate a new `access_token`. Just add this into your script as a first steps. – freeek Oct 01 '19 at 15:01
  • Thanks man! Could you provide me the code with the refresh_token part? Im unable to add in to my script –  Oct 01 '19 at 16:18
  • @Thomas updated. Please, read the docs for more info, you have all the needed links :) – freeek Oct 01 '19 at 16:31
  • you are my champ! –  Oct 01 '19 at 16:34
  • @Thomas you are welcome. Accept an answer if it helped, so that others can also use this solution. – freeek Oct 01 '19 at 16:42
  • Im still trying to figure it out, for now im not able to include it :D (its not working for now) hehe –  Oct 01 '19 at 16:49