3

我正在开发具有 PHP 前端和 java 后端的应用程序,并且应该从前端授权用户离线访问他的 Google Drive。应用程序应存储 Google 访问令牌和刷新令牌并在以后使用它,而无需用户明确允许它。问题是 java 应用程序无法下载从 Google Drive 创建的文件,并且我总是收到以下错误消息“超出未经身份验证的使用的每日限制。继续使用需要注册。”。但是,上传文件和下载从该客户端应用程序上传的文件没有问题。

PHP端的代码如下所示:

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('xxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxx');
$client->setRedirectUri('http://localhost/google-test/index.php');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
print "AccessToken:\n$accessToken\n\n";
?>

我收到以下输出

{"access_token":"ya29.AHES6ZRw6aUxSAlwunKlgJ1pWu0PD1tise_cu7IshJ8_Mg8",
"token_type":"Bearer", "expires_in":3600, 
"refresh_token":"1\/ZEU4mGWxEnrqogPVml3cHc5s9okqHplVZc3FhSF5sac",
"created":1379705627}

用于验证用户并访问其 Google 文档的 Java 代码:

GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder();
credentialBuilder.setJsonFactory(new JacksonFactory());
credentialBuilder.setTransport(new NetHttpTransport());
credentialBuilder.setClientSecrets("xxxxxx.apps.googleusercontent.com","xxxxxx");
Credential credentials = credentialBuilder.build();
//credentials.setAccessToken("ya29.AHES6ZRw6aUxSAlwunKlgJ1pWu0PD1tise_cu7IshJ8_Mg8");
credentials.setRefreshToken("1/ZEU4mGWxEnrqogPVml3cHc5s9okqHplVZc3FhSF5sac");

Drive drive =  new Builder(HTTP_TRANSPORT,  JSON_FACTORY,   credentials)
.setApplicationName("API Project").build();
String query = "mimeType = 'application/vnd.google-apps.document'";
try {
    Drive.Files.List request=drive.files().list();
    FileList tempFileList = request.setQ(query).setMaxResults(1000).execute();
    List<com.google.api.services.drive.model.File> files=tempFileList.getItems();
    for(com.google.api.services.drive.model.File file:files){
     GenericUrl url = new GenericUrl(file.getDownloadUrl());
      HttpResponse response = service.getRequestFactory().buildGetRequest(url)
          .execute();
    } 

我可以访问这些文件中的每一个并输出其标题,但是一旦我尝试下载它,就会收到以下错误:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires  signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

有谁知道这可能是什么问题?我尝试了几种不同的方法,但总是遇到同样的问题。

谢谢

4

0 回答 0