0

I'm just toying around with the google drive api (cloud storage) and figuring out how things work, I wrote some code in play framework where in global.java onStart access to the drive is gained and a test document is written to the drive. Locally this works fine but when I deploy my code to a new CloudBees app and visit the app I get this error in my log and I just can't figure out how to debug it:

Play server process ID is 7454
Oops, cannot start the server.
@6eonea90e: Cannot init the Global object
    at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:57)
    at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:51)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
    at play.api.WithDefaultGlobal$class.play$api$WithDefaultGlobal$$globalInstance(Application.scala:50)
    at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance$lzycompute(Application.scala:383)
    at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance(Application.scala:383)
    at play.api.WithDefaultGlobal$class.global(Application.scala:66)

Link to complete CloudBees log

This is the code of Global.java:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.FileList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import play.*;
import play.Logger;

public class Global extends GlobalSettings {

    private static String CLIENT_ID = "595172328396-l4kpto8ip9fpaea0k2987eeq8f42bged.apps.googleusercontent.com";
    private static String CLIENT_SECRET = "EvTUvAodjGx2eW_d3k8oy8Fb";

    private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

    @Override
    public void onStart(Application app) {    try{
        Logger.info("Application has started");

        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleCredential credential = new GoogleCredential.Builder()
                .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                .setJsonFactory(jsonFactory).setTransport(httpTransport).build()
                .setRefreshToken("1/MZ4GTNA_HMbOcKDSqp6ymSd11dlkgxoMXxfWwhwMJRg").setAccessToken("ya29.AHES6ZQk7NDC-OCba7_yANc_uqWPLwDLl95TlT_DXgkLqrr6qmyLRw");;




        //Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        //Insert a file
        File body = new File();
        body.setTitle("New Document");
        body.setDescription("A test document");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();

        Logger.info("List of stuff: " + service.children().toString());

        Logger.info("File ID: " + file.getId());    }catch (IOException ex) {}
    }
}
4

1 回答 1

0

谷歌访问令牌只能使用 1 小时。似乎您实际上并没有使用刷新令牌请求访问令牌......相反,您似乎正在设置一个已经发布的令牌。

您需要请求获取访问令牌(使用刷新令牌)

“有趣”的是,一旦你使用刷新令牌生成新的访问令牌,旧的就会失效......或者至少这是我发现的经验......

于 2013-07-06T08:38:17.023 回答