我只是在玩谷歌驱动api(云存储),弄清楚它是如何工作的,我写了一些代码在玩框架,在global.java onStart中可以访问驱动器,并向驱动器写入测试文档。在本地,这很好,但当我将代码部署到一个新的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)
链接以完成CloudBees日志
这是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) {}
}
}
谷歌访问令牌的有效期仅为1小时。看起来你实际上并不是在用刷新令牌请求访问令牌。。。相反,你似乎在设置一个已经发布的。
您需要请求获取访问令牌(使用刷新令牌)
"有趣"的是,一旦您使用刷新令牌生成新的访问令牌,旧的访问令牌就会失效。。。或者至少这是我发现的经验。。。