简体   繁体   English

因此,当我将代码部署到CloudBees global.java时将无法运行(Play Framework 2.1)

[英]So when I deploy my code to CloudBees global.java won't run (Play Framework 2.1)

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. 我只是在玩弄Google Drive API(云存储)并弄清楚它是如何工作的,我在play框架中编写了一些代码,其中在global.java onStart中获得了对驱动器的访问权限,并将测试文档写入了该驱动器。 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: 在本地这可以正常工作,但是当我将代码部署到新的CloudBees应用程序并访问该应用程序时,我在日志中收到此错误,而我只是想不出如何对其进行调试:

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 链接到完整的CloudBees日志

This is the code of Global.java: 这是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) {}
    }
}

The google access token is only good for 1 hour. Google访问令牌仅可使用1个小时。 It does not seem as if you are actually requesting an access token with your refresh token... Instead it seems like you are setting one that was already issued. 似乎您实际上并没有在请求带有刷新令牌的访问令牌。相反,似乎您正在设置已经发出的令牌。

You need to request an access token be fetched (using the refresh token) 您需要请求获取访问令牌(使用刷新令牌)

The "fun" is that once you use the refresh token to generate a new access token, the old ones are invalidated... Or at least that is the experience I have found... “乐趣”是,一旦您使用刷新令牌生成新的访问令牌,旧的访问令牌就会失效……或者至少这是我发现的经验……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM