2013-08-22 35 views
2

關於如何通過在java中使用新的Google +域API來列出我的域中的所有活動?如何列出特定谷歌域中的所有活動?

開發者的現場錄像顯示,在4:00分鐘標記,你可以做這樣的事情:

Plus.Activities.List listActivities = plus.activities().list("me", "domain"); 

此代碼的鏈接是here

但是當我真正運行同一行代碼時,它顯示出以下錯誤。

{ 
    "code" : 400, 
    "errors" : [ { 
    "domain" : "global", 
    "location" : "collection", 
    "locationType" : "parameter", 
    "message" : "Invalid string value: 'domain'. Allowed values: [user]", 
    "reason" : "invalidParameter" 
    } ], 
    "message" : "Invalid string value: 'domain'. Allowed values: [user]" 
} 

錯誤有意義的,因爲在activities.list documentation它說,「用戶」是收藏,而不是唯一可接受值「域」。

那麼我該怎麼處理這個問題呢?

回答

1

正如你所說,唯一可用的方法是按當前登錄的用戶列出帖子。您必須使用用戶委派(包含服務帳戶)並循環域中的所有用戶才能獲得所有已發佈的活動。

您可以在響應中使用updated字段來檢查用戶的活動列表中是否有新內容。

這條思路適用於整個Domains API:每個操作都代表用戶完成,沒有超級用戶的「admin」帳戶。這可能是對大量用戶採取行動時的一個限制,因爲您不得不依次對每個用戶進行身份驗證(如果有人有關於如何以更高效的方式實現此目的的想法,請分享!)

+0

代碼片段不正確,並且已更新。的確,每個操作都必須以用戶的名義完成。爲了澄清,域範圍委派是您可以代表域中的每個用戶對應用進行身份驗證的一種方式,以便每個用戶不需要登錄。但是,要使用身份驗證,您必須指定您的帳戶希望代表一個人一次行事。 – Joanna

0

由於文檔賽斯,只有「公開」是可以的:

https://developers.google.com/+/api/latest/activities/list

然而,即使使用的API文檔中提供的示例代碼,通過身份驗證成功後,去我得到0的活動。

/** List the public activities for the authenticated user. */ 
    private static void listActivities() throws IOException { 
    System.out.println("Listing My Activities"); 
    // Fetch the first page of activities 
    Plus.Activities.List listActivities = plus.activities().list("me", "public"); 
    listActivities.setMaxResults(100L); 
    // Pro tip: Use partial responses to improve response time considerably 
    listActivities.setFields("nextPageToken,items(id,url,object/content)"); 

    ActivityFeed activityFeed = listActivities.execute(); 
    // Unwrap the request and extract the pieces we want 
    List<Activity> activities = activityFeed.getItems(); 
    System.out.println("Number of activities: " + activities.size()); 
    // Loop through until we arrive at an empty page 
    while (activities != null) { 
     for (Activity activity : activities) { 
     System.out.println("ID " + activity.getId() + " Content: " + 
       activity.getObject().getContent()); 
     } 

     // We will know we are on the last page when the next page token is null. 
     // If this is the case, break. 
     if (activityFeed.getNextPageToken() == null) { 
     break; 
     } 

    // Prepare to request the next page of activities 
     listActivities.setPageToken(activityFeed.getNextPageToken()); 

     // Execute and process the next page request 
     activityFeed = listActivities.execute(); 
     activities = activityFeed.getItems(); 
    } 
} 

有人知道如何讓這個工作?

0

當您使用Google+ API時,您必須使用「公開」,但是當您使用Google+域名API時,您必須使用「用戶」參數值。

相關問題