我正在进行一个经过身份验证的调用,通过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());
}
}发布于 2010-10-28 03:59:44
我采用了一种不同的方法来解决这个问题。
这就是我解决这个问题的方法。我没有使用GET方法,而是使用了photoSetsInterface (photoSetsInterface.getList(auth.getUser().getId()).getPhotosets()) .For中的getList方法,您可以将auth_token作为输入参数进行传递。因此,它会给你所有的照片集。然后,我拍摄了每个照片集,并检索了所有隐私级别下的图像。
然后,您可以使用PhotosInterface中的getNotInSet方法拍摄不在集合中的所有照片。无论隐私级别如何,getNotInSet方法都会返回不在一个集合中的所有照片。
您可以在我的blog中找到示例代码。
发布于 2011-01-12 07:01:58
可以通过编辑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。
发布于 2013-03-27 01:28:28
要使用flickrj api进行经过身份验证的调用,除了apiKey和secret之外,还需要第三个代码,即令牌。
我们通过一次性的第一次调用获得令牌,然后有了令牌,您就可以像预期的那样使用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个密钥( Flickr,secret,token),这里是我的服务类,其中包含创建apiKey对象的静态方法。
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;
}
}https://stackoverflow.com/questions/3289552
复制相似问题