PHP 上传到 imgur 相册不起作用?

PHP upload to a imgur Album does not work?

我正在尝试上传到我的 imgur 帐户的相册,然而,上传有效,但它看起来像要上传为 anonym/public?

这是我的代码:

<?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);

有什么建议吗?

如果要添加图片到相册,根据doc,需要通过相册id。确保您已生成可以访问秘密相册的令牌。 你可以找到一些关于代币的技巧。

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

您可以使用 this api 查看相册 ID。

要刷新令牌:

如果用户已授权他们的帐户,但您不再拥有对他们有效的 access_token,则可以使用 refresh_token.

生成一个新帐户

要获取新的访问令牌,您的应用程序会执行 POSThttps://api.imgur.com/oauth2/token。请求必须包含以下参数才能使用刷新令牌:

refresh_token:授权码交换返回的refresh token

client_id:申请注册时获得的client_id

client_secret:申请注册时获取的client secret。

grant_type:根据 OAuth2 规范的定义,该字段必须包含一个值:refresh_token.

只要用户没有撤​​销授予您的应用程序的访问权限,响应就会包含一个新的访问令牌。此类请求的响应如下所示:

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

在脚本开头添加刷新部分。类似于:

$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;