简体   繁体   English

将照片上传到flickr(Java)

[英]Uploading photos to flickr (Java)

I'm a beginner trying to figure out the Flickr API. 我是一个初学者,试图弄清楚Flickr API。 I'm trying to create a simple app that uploads pics to Flickr. 我正在尝试创建一个简单的应用程序,将照片上传到Flickr。

While trying to send my request to upload a photo I get an "Invalid signature" response. 尝试发送我的上传照片请求时,收到“无效签名”响应。

This is my base string for my signature: 这是我签名的基本字符串:

POST&http%3A%2F%2Fapi.flickr.com%2Fservices%2Fupload&oauth_consumer_key%3D3f1e1948c1db9a45ca95febef6f5590e%26oauth_nonce%3D6151b3221a15f56c82ba0df47d57637e%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1366133923%26oauth_token%3D72157633261587382-7dcbe2359dbcfb06%26oauth_version%3D1.0

I basically did the same thing I did when I requested the user's authentication, but with the access token as an argument. 我基本上做了与我请求用户身份验证时所做的相同的事情,但是使用访问令牌作为参数。

This is my code for the request: 这是我的请求代码:

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

import org.apache.commons.codec.DecoderException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {

  /**
   * @param args
 * @throws DecoderException 
 * @throws SignatureException 
 * @throws NoSuchAlgorithmException 
   */
  public static void main(String[] args) throws NoSuchAlgorithmException, SignatureException, DecoderException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://api.flickr.com/services/upload");

    try {
      FileBody bin = new FileBody(new File("/Users/[MYUSERNAME]/Desktop/Untitled-1.png"));

      MultipartEntity reqEntity = new MultipartEntity();
      reqEntity.addPart("photo", bin);

      reqEntity.addPart("api_key", new StringBody(Utils.consumer_key));
      reqEntity.addPart("oauth_token", new StringBody(Utils.oauth_token));
      reqEntity.addPart("api_sig", new StringBody(Request.sig()));
      httppost.setEntity(reqEntity);

      System.out.println("Requesting : " + httppost.getRequestLine());
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      String responseBody = httpclient.execute(httppost, responseHandler);

      System.out.println("responseBody : " + responseBody);

    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      httpclient.getConnectionManager().shutdown();
    }
  }

}

Do I have a problem with my understanding of sending a MIME request, or is it just the signature? 我对发送MIME请求的理解是否有问题,还是仅仅是签名? If so, how should I construct my signature? 如果是这样,我应该如何构造我的签名? Thank you. 谢谢。

Basically to do anything with the Flickr api you need some type of authorization "token" - and depending on how you go about authenticating, you may get different authorization tokens. 基本上,要使用Flickr api进行任何操作,都需要某种类型的授权“令牌”-并且根据您进行身份验证的方式,您可能会获得不同的授权令牌。 There are 2 types of authorization tokens: 授权令牌有两种类型:

  1. oauth token - if you do oauth authentication oauth令牌 -如果您执行oauth身份验证
  2. auth token - if you use the old deprecated authentication method auth令牌 -如果您使用旧的不赞成使用的身份验证方法

Flickr's api documentation only shows how to upload using auth token (#2), but it is also possible to upload using Oauth to get an oauth token (just not documented). Flickr的api文档仅显示如何使用auth令牌 (#2)上传,但也可以使用Oauth进行上传以获取oauth令牌 (只是没有记录)。 Your issue seems to be that you are mixing up your ouath token with Flickr's auth token method of uploading. 您的问题似乎是,您要混合使用ouath令牌和Flickr的auth令牌上传方法。

If you are going to use Oauth authentication you will need to pass Oauth equivalents of the api_key and api_sig , which are oauth_consumer_key and oauth_signature . 如果要使用Oauth身份验证,则需要传递api_keyapi_sig的 Oauth等效 ,即oauth_consumer_keyoauth_signature In addition, a proper Oauth payload should contain oauth_timestamp , oauth_nonce , oauth_signature_method and oauth_version . 此外,适当的Oauth有效负载应包含oauth_timestampoauth_nonceoauth_signature_methodoauth_version

One other thing to note: you also need to pass perms=write param in your original Oauth authentication so that a write permission is enabled for your oauth token - otherwise you will lack permissions to upload. 需要注意的另一件事:您还需要在原始Oauth身份验证中传递perms = write参数,以便为您的oauth令牌启用写权限-否则,您将没有上传权限。

I believe the problem is with the FileBody Constructor 我相信问题在于FileBody构造函数

FileBody bin = new FileBody(new File("/Users/[MYUSERNAME]/Desktop/Untitled-1.png")); FileBody bin = new FileBody(new File(“ / Users / [MYUSERNAME] /Desktop/Untitled-1.png”));

If this constructor is used than the result of MultiPart Encoding is 如果使用此构造函数,则MultiPart Encoding的结果为

Content-Disposition: form-data; 内容处置:表单数据; name="photo"; name =“ photo”;

Whereas the expected request should be of the form 预期请求应采用以下形式:

Content-Disposition: form-data; 内容处置:表单数据; name="photo"; name =“ photo”; filename="Untitled-1.png" filename =“ Untitled-1.png”

So the correct constructor to be used here is 所以这里要使用的正确构造函数是

new FileBody(new File("/Users/[MYUSERNAME]/Desktop/Untitled-1.png"),ContentType.APPLICATION_OCTET_STREAM, "Untitled-1.png") 新FileBody(新File(“ / Users / [MYUSERNAME] /Desktop/Untitled-1.png”),ContentType.APPLICATION_OCTET_STREAM,“ Untitled-1.png”)

Although i haven't tried executing your code myself but hopefully this should fix your problem. 虽然我没有尝试自己执行代码,但希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM