我是一个试图弄清楚 Flickr API 的初学者。我正在尝试创建一个将图片上传到 Flickr 的简单应用程序。
在尝试发送上传照片的请求时,我收到“无效签名”响应。
这是我签名的基本字符串:
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
当我请求用户的身份验证时,我基本上做了同样的事情,但使用访问令牌作为参数。
这是我的请求代码:
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();
}
}
}
我对发送 MIME 请求的理解是否有问题,还是只是签名?如果是这样,我应该如何构建我的签名?谢谢你。