gpt4 book ai didi

java - 超出 Google+ Java 客户端 API 速率限制

转载 作者:行者123 更新时间:2023-12-01 13:23:59 27 4
gpt4 key购买 nike

每次我尝试获取经过身份验证的用户在 Google+ 上的好友列表时,都会遇到“超出用户速率限制”错误。查看我的 API 控制台,我可以看到我的项目在过去 24 小时内提交了 185 个请求,并在同一时间范围内出现了 154 个错误。

奇怪的是,只有当我尝试获取经过身份验证的用户的 friend 而不是他们的个人资料信息时,才会发生这种情况。

我做错了什么吗?

这是我的代码:

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(),这样就消除了速率限制错误。

很奇怪,不是吗?

关于java - 超出 Google+ Java 客户端 API 速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com