如何通过Flickrj Api访问私人照片?
我正在通过 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());
}
}
I'm making an authenticated call to access photos through Flickr API. But I am only getting my public photos but not any private photos.
Given below is the code I'm using,
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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以通过编辑 flickrj 来访问私人数据。
我在
com.aetrion.flickr.photos.PhotosInterface
中添加了auth_token
参数:通过此更改,我能够获取我的私人照片。
flickr api 文档说,“每个经过身份验证的调用都需要
auth_token
和api_sig
参数< /em> “.. 那个东西不见了。我从那里得到了这个想法: 链接文本 并使用 servlet 访问 flickr 。
It is possible to access private data by editing flickrj.
I added the
auth_token
parameter incom.aetrion.flickr.photos.PhotosInterface
:with this change, I was able to get my private photos.
the flickr api docs say, that " Every authenticated call requires both the
auth_token
andapi_sig
arguments ".. and that was missing.I took the idea from there: link text and used a servlet to access 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 的唯一目的是使用它来构造身份验证 URL,请参见第 60 行:
URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
我在 Spring MVC 应用程序中执行此操作,而不是桌面应用程序,因此在第 60 行之后,我将 URL 打印到 System.out,然后使用 Thread.sleep(60000); 将程序暂停一分钟,
这给了我足够的时间将 URL 从控制台复制粘贴到浏览器,然后单击“确定允许我的应用程序使用我的 flickr 帐户” ”。
之后,当程序恢复时,我所关心的只是第 70 行,该行将令牌打印到控制台。
有了令牌在手,授权就完成了,我可以使用 Flickrj api。
有了 3 个密钥(apiKey、secret、token),这里是我的 Service 类,它具有用于创建 Flickr 对象的静态方法。
To make authenticated calls with the flickrj api you need, in addition to your apiKey and secret, a third code, the token.
We get the token with a one-off first call, and then with the token in hand you can just use the api classes as you'd expect.
If you've ever used a flickr iOS/Android client you will recognise this: the first time you use the app, it sends you to a URL for you to authorise it. What it does is when you authorise it it gets this token key, and then its good to go. Without it, its not. Same for your java app.
I used this example implementation for this first step authentication, with a couple of modifications though as I am working in a Spring MVC app, not a desktop app.
Remember that you only run this once, and the only point of this is to get the token key.
https://github.com/mohlendo/flickrj/blob/master/examples/AuthExample.java
Line 55:
frob = authInterface.getFrob();
The only purpose of a frob is to use it to construct the authentication URL, see line 60:
URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
I did this in a spring MVC app, not a desktop app, so after line 60 I instead printed the URL to System.out and then paused the programme for a minute with
Thread.sleep(60000);
That gave me enough time to copy-paste the URL from my console to a browser and click "OK allow my app to use my flickr account".
Straight after that, when the program resumes all I cared was line 70 that prints the token to the console.
With the token in hand, the authorisation is complete and I was set to use the Flickrj api.
With the 3 keys in hand (apiKey, secret, token) here is my Service class with a static method to create a Flickr object.
我不知道这是否对您有帮助,但我也遇到了类似的问题,只能看到公开照片,并且已经解决了。
我从以下位置下载了 flickrapi-1.2.zip: http://sourceforge.net/projects/flickrj /files/ 并在我的网络应用程序中使用 flickrapi-1.2.jar。
Flickr.debugRequest 显示了 GET 请求,但它从未包含 AuthInterface checkToken(例如)成功强制请求的“auth_token”、“api_sig”等参数。
无论如何,今天我从以下位置下载了源代码:
flickrj.cvs.sourceforge.net/flickrj/
(您可以在以下位置查看修订历史记录:flickrj.cvs.sourceforge.net/viewvc/flickrj/api/build.properties?view=如果您有兴趣,请登录。)
我在本地构建了 jar,将其包含在网络应用程序中,并且可以看到私人照片。
我还没有花时间进一步调查确切的问题是什么……需要用它来做点什么:)
我不知道这是否会对你有帮助,但这个发现是唯一让我远离疯狂的东西。
I don't know if this will help you, but I was having a similar problem with seeing only public photos and have since got it working.
I'd downloaded the flickrapi-1.2.zip from: http://sourceforge.net/projects/flickrj/files/ and was using flickrapi-1.2.jar in my webapp.
Flickr.debugRequest was showing the GET request, but it never included "auth_token", "api_sig", etc. parameters that AuthInterface checkToken (for example) successfully forces on the request.
Anyway, so just today I downloaded the source from:
flickrj.cvs.sourceforge.net/flickrj/
(You can see the revision history at: flickrj.cvs.sourceforge.net/viewvc/flickrj/api/build.properties?view=log if you're interested.)
I built the jar locally, included that in the web app and could see private photos.
I haven't spent the time to investigate further about what the exact problem was...need to use it for something :)
I don't know if that will help you, but that find is the only thing separating me from insanity.
我通过采用不同的方法设法解决了这个问题。
这就是我解决这个问题的方法。我没有使用 GET 方法,而是使用 photoSetsInterface (photoSetsInterface.getList(auth.getUser().getId()).getPhotosets()) 中的 getList 方法。对于此方法,您可以将 auth_token 作为输入参数传递。因此它为您提供了所有照片集。然后我拍摄了每张照片并在所有隐私级别下检索图像。
然后您可以使用 PhotosInterface 中的 getNotInSet 方法拍摄不在集合中的所有照片。 getNotInSet 方法返回不在集合中的所有照片,无论隐私级别如何。
您可以在我的博客。
I managed to solve this problem by following a different approach.
This is how i solved this. Instead of using the GET method I used the getList method in photoSetsInterface (photoSetsInterface.getList(auth.getUser().getId()).getPhotosets()) .For this method you can pass the auth_token as an input parameter. Therefore it gives you all the photo sets. Then I took each photoset and retrived images under all privacy levels.
And then you can take all the photos that are not in a set by using the getNotInSet method in PhotosInterface. getNotInSet method returns all the photos not in a set regardless of the privacy level.
You can find the sample code in my blog.