我正在通过Flickr API进行身份验证调用以访问照片.但我只收到我的公开照片,但没有任何私人照片.
下面是我正在使用的代码,
Flickr f;
RequestContext requestContext;
String frob = "";
String token = "";
DocumentBuilder xmlParser = null;
public void getImages() throws ParserConfigurationException, IOException, SAXException, FlickrException, URISyntaxException, NoSuchAlgorithmException
{
DocumentBuilderFactory dcb = DocumentBuilderFactory.newInstance();
try {
this.xmlParser = dcb.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
f = new Flickr("199d038ad88f6c6c377a4ab2341fb60f","4583b2386d3d6439",new REST()) ;
Flickr.debugStream = false;
requestContext = RequestContext.getRequestContext();
AuthInterface authInterface = f.getAuthInterface();
//PeopleInterface peopleInterface = f.getPeopleInterface();
try {
frob = authInterface.getFrob();
} catch (FlickrException e) {
e.printStackTrace();
}
System.out.println("frob: " + frob);
java.net.URL url =authInterface.buildAuthenticationUrl(Permission.READ, frob);
System.out.println(url.toExternalForm());
Desktop desktop = Desktop.getDesktop();
desktop.browse(url.toURI());
// Get the response
Auth auth = null ;
String aLine = "";
while(aLine.equals(""))
{
java.io.DataInputStream in = new java.io.DataInputStream(System.in);
aLine = in.readLine();
}
auth =authInterface.getToken(frob);
System.out.println("auth = "+auth);
requestContext = RequestContext.getRequestContext();
requestContext.setAuth(auth);
f.setAuth(auth);
UrlsInterface urlsInterface = f.getUrlsInterface();
PhotosInterface photosInterface = f.getPhotosInterface();
SearchParameters searchParams=new SearchParameters();
searchParams.setSort(SearchParameters.INTERESTINGNESS_DESC);
//Execute search with entered tags
searchParams.setUserId(auth.getUser().getId());
PhotoList photoList=photosInterface.search(searchParams, 10,1);
if(photoList!=null){
//Get search result and check the size of photo result
for(int i=0;i<photoList.size();i++){
Photo photo=(Photo)photoList.get(i);
System.out.println(photo.getSmallSquareUrl());
}
}
最佳答案 可以通过编辑flickrj来访问私有数据.
我在com.aetrion.flickr.photos.PhotosInterface中添加了auth_token参数:
public PhotoList search(SearchParameters params, int perPage, int page)
throws IOException, SAXException, FlickrException {
PhotoList photos = new PhotoList();
List parameters = new ArrayList();
parameters.add(new Parameter("method", METHOD_SEARCH));
parameters.add(new Parameter("api_key", apiKey));
parameters.addAll(params.getAsParameters());
if (perPage > 0) {
parameters.add(new Parameter("per_page", "" + perPage));
}
if (page > 0) {
parameters.add(new Parameter("page", "" + page));
}
//
String token = RequestContext.getRequestContext().getAuth().getToken();
if (token != null)
parameters.add(
new Parameter(
"auth_token",
RequestContext.getRequestContext().getAuth().getToken()
)
);
//
parameters.add(
new Parameter(
"api_sig",
AuthUtilities.getSignature(sharedSecret, parameters)
)
);
Response response = transport.get(transport.getPath(), parameters);
通过这种改变,我得到了我的私人照片.
flickr api文档说,“每个经过身份验证的调用都需要auth_token和api_sig参数”..并且缺少.
我从那里接受了这个想法:link text并使用servlet访问flickr.