Question

Every time I attempt to get a list of the authenticated user's friends on Google+, I'm faced with a "User Rate Limit Exceeded" error. Looking at my API console, I can see that my project has submitted 185 requests in the last 24 hours, and was given 154 errors in the same timeframe.

The strange thing is, this only happens when I try to get the authenticated user's friends, but not their profile information.

Am I doing anything blatantly wrong?

Here's my code:

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();
        }
    }
}

For completion's sake, here's my View class:

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));
    }

}

EDIT: Forgot to include the Constants class (extraneous values omitted):

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
Was it helpful?

Solution

SOLVED:

Instead of using Collections.singleton(PlusScopes.PLUS_ME), I used PlusScopes.all() and that got rid of the Rate Limit error.

Strange, isn't it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top