2010-07-20 61 views
4

我正在通過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()); 

     } 

    } 

回答

0

我設法通過遵循不同的方法來解決這個問題。

這就是我解決這個問題的方法。而不是使用GET方法,我在photoSetsInterface(photoSetsInterface.getList(auth.getUser()。getId())。getPicturesets())中使用getList方法。對於此方法,您可以將auth_token作爲輸入參數傳遞。因此它給你所有的照片集。然後我在每個隱私級別下拍攝了每張照片和圖片。

然後,您可以通過在PhotosInterface中使用getNotInSet方法獲取不在集合中的所有照片。無論隱私級別如何,getNotInSet方法都會返回不在集合中的所有照片。

您可以在我的blog中找到示例代碼。

0

,但我是有隻看到公開的照片有類似的問題,並有因爲得到它的工作。

我下載了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應用程序中,可以看到私人照片。

我沒有花,以進一步調查有關的具體問題是什麼?需要用它的東西的時候:)

我不知道這是否會幫助你,但發現是把我從瘋狂中分離出來的唯一事情。

1

通過編輯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_tokenapi_sig參數」 ..而失蹤。

我採取了這樣的想法:link text並使用一個servlet訪問flickr。

1

要使用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; 
    } 
}