如何通过Flickrj Api访问私人照片?
我正在通过Flickr API进行经过验证的电话访问照片。但我只获取我的公开照片,但没有任何私人照片。如何通过Flickrj 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());
}
}
我设法通过遵循不同的方法来解决这个问题。
这就是我解决这个问题的方法。而不是使用GET方法,我在photoSetsInterface(photoSetsInterface.getList(auth.getUser()。getId())。getPicturesets())中使用getList方法。对于此方法,您可以将auth_token作为输入参数传递。因此它给你所有的照片集。然后我在每个隐私级别下拍摄了每张照片和图片。
然后,您可以通过在PhotosInterface中使用getNotInSet方法获取不在集合中的所有照片。无论隐私级别如何,getNotInSet方法都会返回不在集合中的所有照片。
您可以在我的blog中找到示例代码。
,但我是有只看到公开的照片有类似的问题,并有因为得到它的工作。
我下载了flickrapi-1.2.zip:http://sourceforge.net/projects/flickrj/files/,并在我的webapp中使用flickrapi-1.2.jar。
Flickr.debugRequest显示GET请求,但它从未包含“auth_token”,“api_sig”等参数,AuthInterface checkToken(例如)成功地强制请求参数。
无论如何,所以就在今天我从网上下载源:
flickrj.cvs.sourceforge.net/flickrj/
(你可以看到修订历史记录在:flickrj.cvs.sourceforge.net/如果你有兴趣,可以使用viewvc/flickrj/api/build.properties?view = log。)
我在本地构建了jar,包括在web应用程序中,可以看到私人照片。
我没有花,以进一步调查有关的具体问题是什么?需要用它的东西的时候:)
我不知道这是否会帮助你,但发现是把我从疯狂中分离出来的唯一事情。
通过编辑flickrj可以访问私人数据。这种变化
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);
,我能得到我的私人照片:
我加入了auth_token
参数com.aetrion.flickr.photos.PhotosInterface
。
Flickr的API文档说,这 “每个认证的呼叫既需要auth_token
和api_sig
参数” ..而失踪。
我采取了这样的想法:link text并使用一个servlet访问flickr。
要使用flickrj API进行身份验证呼叫,除了您的apiKey和秘密,还需要第三个代码代币。
我们通过一次性第一次调用获得令牌,然后使用令牌,您可以像期望的那样使用API类。
如果您曾经使用过flickr iOS/Android客户端,您将会认识到这一点:您第一次使用该应用程序时,会将您发送给您的URL以供您授权。它的作用是当你授权它时,它获得这个密钥,然后它很好。没有它,它不是。你的Java应用程序也是如此。
我使用此示例实现进行第一步身份验证,但有一些修改,因为我在Spring MVC应用程序中工作,而不是桌面应用程序。 请记住,您只运行一次,并且唯一的一点是获取令牌密钥。
https://github.com/mohlendo/flickrj/blob/master/examples/AuthExample.java
线55:frob = authInterface.getFrob();
一个FROB的唯一目的是用它来构建验证网址,请参阅第60行:URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
我这样做一个春天的MVC应用程序,而不是桌面应用程序,所以在第60行之后,我将URL打印到System.out,然后用Thread.sleep(60000);
这给了我足够的时间将URL从我的控制台复制粘贴到浏览器,然后单击“确定允许我的应用使用我的flickr帐户”。
在此之后,当程序重新开始时,我关心的是第70行将令牌打印到控制台。
随着令牌的掌握,授权已完成,我被设置为使用Flickrj api。
手中有3把钥匙(apiKey,secret,token),这里是我的Service类,用静态方法创建一个Flickr对象。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.xml.parsers.ParserConfigurationException;
import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.RequestContext;
import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.auth.Permission;
import com.aetrion.flickr.util.IOUtilities;
public class FlickrjService {
private static Flickr flickr;
public static Flickr getFlickr() throws ParserConfigurationException, IOException {
InputStream in = null;
Properties properties;
try {
in = FlickrjService.class.getResourceAsStream("/flickrj.properties");
properties = new Properties();
properties.load(in);
} finally {
IOUtilities.close(in);
}
flickr = new Flickr(properties.getProperty("flickrj.apiKey"), properties.getProperty("flickrj.secret"), new REST("www.flickr.com"));
RequestContext requestContext = RequestContext.getRequestContext();
Auth auth = new Auth();
auth.setPermission(Permission.READ);
auth.setToken(properties.getProperty("flickrj.token"));
requestContext.setAuth(auth);
Flickr.debugRequest = false;
Flickr.debugStream = false;
return flickr;
}
}