使用TRESTClient上传图片到imgur不起作用。

3

我想将一个jpg文件上传到imgur并获取该jpg的链接。

我有imgur API的客户端ID和客户端密钥。

以下是Delphi代码:

procedure TfrmMain.Button6Click(Sender: TObject);
var
  client: TRESTClient;
  request: TRESTRequest;
  response: TCustomRESTResponse;
begin
  client := TRESTClient.Create(nil);
  try
    client.BaseURL := 'https://api.imgur.com/';
    Client.AddParameter('Client ID', '...', TRESTRequestParameterKind.pkHTTPHEADER);
    Client.AddParameter('Client Secret', '...', TRESTRequestParameterKind.pkHTTPHEADER);
    request := TRESTRequest.Create(nil);
    try
      request.Client := client;
      request.Method := rmPOST;
      request.Resource := 'a/C11W7xC';
      request.Accept := 'application/json';
      request.AddParameter('image','D:\linedw.jpg' , pkFile);
      request.Execute;
      response := request.Response;
      if response.Status.Success then
      begin
        mo_response.Lines.add('Success: ' + slinebreak + response.Content);
      end
      else
      begin
          mo_response.Lines.add('Failed ' +  response.StatusText + ': ' + slinebreak + response.Content);
      end;
    finally
      request.Free;
    end;
  finally
   client.Free;
  end;
end;

response.Content 的错误信息如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>imgur: the simple 404 page</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="robots" content="noindex,nofollow" />
    <meta name="keywords" content="images, funny pictures, image host, image upload, image sharing, image resize" />
    <meta name="description" content="Imgur is home to the web's most popular image content, curated in real time by a dedicated community through commenting, voting and sharing." />
    <meta name="copyright" content="Copyright 2014 Imgur, Inc." />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge;" />
    <link rel="stylesheet" type="text/css" href="https://s.imgur.com/min/404.css?1393899213" />
    <!--[if IE 9]><link rel="stylesheet" href="https://s.imgur.com/include/css/ie-sucks.css?0" type="text/css" /><![endif]-->
</head>
<body>
    <div class="nodisplay">
        Imgur is home to the web's most popular image content, curated in real time by a dedicated community through commenting, voting and sharing.
    </div>

    <div id="hallway">
        <div class="container">
            <div id="cat1" class="painting">
                <img src="//s.imgur.com/images/404/cat1weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="cat2" class="painting">
                <img src="//s.imgur.com/images/404/cat2weyes.png">
                <div class="eye-container">
                    <div class="eye">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="giraffe" class="painting">
                <img src="//s.imgur.com/images/404/giraffeweyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
                <img class="monocle" src="//s.imgur.com/images/404/monocle.png" />
            </div>
            <div id="cat3" class="painting">
                <img src="//s.imgur.com/images/404/cat3weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="cat4" class="painting">
                <img src="//s.imgur.com/images/404/cat4weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="footer textbox">
        <h1>Zoinks! You've taken a wrong turn.</h1>
        <p>Let's split up, gang. If you're looking for an image, it's probably been deleted or may not have existed at all.</p>
        <p>If you are looking for groovy images, <a href="//imgur.com">visit our gallery!</a></p>
        <a href="//imgur.com" class="footer-logo"><img src="https://s.imgur.com/images/imgurlogo-header.png"></a>
    </div>

    <script type="text/javascript">
        (function(widgetFactory) {
            widgetFactory.mergeConfig('analytics', {
                isAdmin: false
            });
        })(_widgetFactory);
    </script>

    <script type="text/javascript" src="https://s.imgur.com/min/404.js?1393899213"></script>
    <script type="text/javascript">
            var e404 = E404.getInstance();
            e404.generalInit();
    </script>
</body>
</html>

我没有调用REST API的经验。我搜索了Delphi演示信息,但没有找到太多。我需要一些关于此的指导。

Delphi 10.4 / Windows 10


3
根据文档,上传图片的资源是request.Resource := '3/upload';。您还应该使用OAuth 2.0通过Authorization: Bearer {YOUR_ACCESS_TOKEN} HTTP头进行身份验证。Imgur 允许您通过Authorization: ClientID {YOUR_CLIENT_ID}匿名上传图片。匿名上传的图片将不会与您的账户绑定。 - Peter Wolf
@PeterWolf 那应该作为答案而不是评论发布 - Remy Lebeau
1
你实际上请求了 https://api.imgur.com/a/C11W7xC,但该网址不存在。仅仅因为你期望返回的是 application/json 并不意味着你就一定会得到它。你所质疑的输出(尽管你的代码中有)并不包含 response.StatusText,这很可能是 Not Found。如果你检查 response.StatusCode,它会告诉你 404 而不是 200。需要最基本的 HTTP 理解。 - AmigoJack
@RemyLebeau 谢谢你帮我修改问题标题。你的建议是正确的,Peter Wolf 的评论就是最终答案。Peter,请你添加一个回答,让我把它标记为正确答案。 - Mitchell Hu
@AmigoJack 你说得对,Status.Text 是“Failed Not Found:”。 最终,我按照Perter Wolf的提示修改了代码,现在它可以正常工作了。 StatusCode = 200,所以我可以解析Response.content JSON字符串并获取图片链接。 谢谢。 - Mitchell Hu
1个回答

2

您正在尝试将图像上传到服务器上的无效资源a/C11W7xC,这就是为什么您会收到HTTP 404未找到响应和HTML内容。

根据文档,上传图像的资源应该是3/upload

我自己没有使用过API,但我觉得您使用的授权与Imgur的授权不符。

Imgur的API允许您通过Authorization: ClientID {YOUR_CLIENT_ID} HTTP头匿名上传图像,或者使用Authorization: Bearer {YOUR_ACCESS_TOKEN} HTTP头将上传的图像与您的帐户绑定。请参见授权和OAuth以了解如何获取访问令牌。

请注意,您不应该将客户端凭据与全世界共享,它的名称中有secret。我建议您此时应该更新您的客户端凭据。


非常感谢你们两位。我已重新生成了密钥值。 - Mitchell Hu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接