[英]Google+ Java Client API Rate Limit Exceeded
每當我嘗試在Google+上獲取經過身份驗證的用戶的朋友的列表時,都會遇到“超出用戶速率限制”錯誤。 查看我的API控制台,我可以看到我的項目在過去24小時內提交了185個請求,並且在同一時間段內收到154個錯誤。
奇怪的是,這僅在我嘗試獲取經過身份驗證的用戶的朋友而不是他們的個人資料信息時發生。
我在做任何公然的錯誤嗎?
這是我的代碼:
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.Plus.Comments;
import com.google.api.services.plus.Plus.Activities.List;
import com.google.api.services.plus.Plus.Moments;
import com.google.api.services.plus.model.Activity;
import com.google.api.services.plus.model.ActivityFeed;
import com.google.api.services.plus.model.Comment;
import com.google.api.services.plus.model.CommentFeed;
import com.google.api.services.plus.model.Moment;
import com.google.api.services.plus.model.MomentsFeed;
import com.google.api.services.plus.model.PeopleFeed;
import com.google.api.services.plus.model.Person;
import com.google.api.services.plus.PlusScopes;
public class Authenticator
{
public static Plus plus;
public Boolean isAuthenticated;
private final File DATA_STORE_DIR = new File(System.getProperty("user.home"), ".store/plus_sample");
private FileDataStoreFactory dataStoreFactory;
private HttpTransport httpTransport;
public Authenticator()
{
try
{
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = authorize();
plus = new Plus.Builder(httpTransport, Constants.JSON_FACTORY, credential).setApplicationName(Constants.APP_NAME).build();
isAuthenticated = true;
}
catch(Exception e)
{
e.printStackTrace();
isAuthenticated = false;
}
}
private Credential authorize() throws Exception
{
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(Constants.JSON_FACTORY, new InputStreamReader(Authenticator.class.getResourceAsStream("/com/example/plustests/client_secrets.json")));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, Constants.JSON_FACTORY, clientSecrets, Collections.singleton(PlusScopes.PLUS_ME)).setDataStoreFactory(dataStoreFactory).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
public void printFriendsList() throws IOException
{
Plus.People.List listPeople = plus.people().list("me", "visible");
PeopleFeed peopleFeed = listPeople.execute();
java.util.List<Person> people = peopleFeed.getItems();
while(people != null)
{
for(Person person : people)
{
View.printString(person.getDisplayName(), "Friend:");
}
if(peopleFeed.getNextPageToken() == null) break;
listPeople.setPageToken(peopleFeed.getNextPageToken());
peopleFeed = listPeople.execute();
people = peopleFeed.getItems();
}
}
}
為了完成,這是我的View類:
package com.example.plustests;
public class View
{
private static final char type1 = '=';
private static final char type2 = '+';
private static final char type3 = '~';
private static final char type4 = '-';
private static final char type5 = ' ';
private static char getSelectedChar(int type)
{
switch(type)
{
case 1: return type1;
case 2: return type2;
case 3: return type3;
case 4: return type4;
case 5: return type5;
default: return ' ';
}
}
public static void header(String name, int type)
{
char selectedChar = getSelectedChar(type);
String pre = "\n", post = " ";
for(int index = 0; index < 18; index++)
{
pre += selectedChar;
post += selectedChar;
}
pre += " ";
System.out.println(pre + name + post);
}
public static void note(String content, Boolean isError)
{
if(isError == false)
System.out.println("NOTE: " + content);
else
System.err.println("ERROR: " + content);
}
public static void separator(int type)
{
char selectedChar = getSelectedChar(type);
String output = "";
for(int index = 0; index < 54; index++)
{
output += selectedChar;
}
System.out.println(output);
}
public static void printString(Object data, String type)
{
System.out.println(type + ": " + (data == null ? "NO DATA" : data));
}
}
編輯:忘記包括Constants類(省略了外部值):
package com.example.plustests;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
public class Constants
{
public static final String APP_NAME = "My Sample API Project";
public static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
}//public class Constants
解決了:
我沒有使用Collections.singleton(PlusScopes.PLUS_ME)
,而是使用了PlusScopes.all()
並擺脫了“速率限制”錯誤。
奇怪,不是嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.