2017-07-04 72 views
0

我需要從我的Gui App中驗證用戶的頁面。我沒有找到任何解決方案,我怎麼用java和restfb庫來做到這一點。我能夠從我的Gui App驗證用戶,但我想訪問用戶的頁面,而不是用戶的牆。Rest fb - 授權url獲取頁面訪問令牌

String domain = "http://seznam.cz/"; 
String appId = "1784741508503328"; 
String authUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&client_id="+appId+"&redirect_uri="+domain+"&scope=user_about_me," 
     + "user_actions.books,user_actions.fitness,user_actions.music,user_actions.news,user_actions.video,user_birthday,user_education_history," 
     + "user_events,user_photos,user_friends,user_games_activity,user_hometown,user_likes,user_location,user_photos,user_relationship_details," 
     + "user_relationships,user_religion_politics,user_status,user_tagged_places,user_videos,user_website,user_work_history,ads_management,ads_read,email," 
     + "manage_pages,publish_actions,read_insights,user_friends,read_page_mailboxes,rsvp_event"; 

此代碼僅適用於用戶的牆/照片等等,但不適用於用戶的頁面。你能幫我怎麼編輯這個URL來獲取頁面訪問令牌嗎?非常感謝。

整個代碼:

public void getAccessToken(){ 
     String domain = "http://seznam.cz/"; 
     String appId = "1784741508503328"; 
     String authUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&client_id="+appId+"&redirect_uri="+domain+"&scope=user_about_me," 
      + "user_actions.books,user_actions.fitness,user_actions.music,user_actions.news,user_actions.video,user_birthday,user_education_history," 
      + "user_events,user_photos,user_friends,user_games_activity,user_hometown,user_likes,user_location,user_photos,user_relationship_details," 
      + "user_relationships,user_religion_politics,user_status,user_tagged_places,user_videos,user_website,user_work_history,ads_management,ads_read,email," 
      + "manage_pages,publish_actions,read_insights,user_friends,read_page_mailboxes,rsvp_event"; 
     System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.get(authUrl); 
     while(true){ 
      if(!driver.getCurrentUrl().contains("facebook.com")){ 
       String url = driver.getCurrentUrl(); 
       accessToken = url.replaceAll(".*#access_token=(.+)&.*", "$1"); 
       PrintWriter p = null; 
       try {p = new PrintWriter("users.txt");} 
       catch (FileNotFoundException ex) {Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);} 
       fbClient = new DefaultFacebookClient(accessToken); 
       if(!accessToken.equals("null")){ 
        p.println(extendToken().getAccessToken() + "," + extendToken().getExpires()); 
       } 

       else{ 
        p.print(""); 
       } 
       driver.quit(); 
       p.close(); 
       return; 
      } 
     } 
} 
+0

這個「code」?它只是字符串,它什麼都不做 – azro

+0

您不會通過此URL獲得頁面訪問令牌。您需要請求必要的權限,然後發出API請求以獲取用戶頁面的訪問令牌。這些文件的工作原理非常清晰。 – CBroe

+0

當然,這段代碼什麼都不做。它只是代碼的一部分,但卻是最關鍵的部分。請參閱編輯 –

回答

-1

這是很容易爲:

Connection<Account> result2 = fbClient.fetchConnection("me/accounts",Account.class); 
for(List<Account> page : result2){ 
     for(Account a : page){ 
      System.out.println(a.getAccessToken()); 
      System.out.println(a.getName()); 
     } 
} 

此代碼將打印出所有的頁面名稱和它們的訪問令牌。你可以簡單地將它們存儲在字符串中,然後使用它們。