每当我尝试在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(),并且摆脱了“速率限制”错误。

奇怪,不是吗?

10-05 22:52