Exception Google docs Java api(例外)

Exception Google docs Java api(例外),exception,nullpointerexception,google-docs,google-docs-api,google-drive-api,Exception,Nullpointerexception,Google Docs,Google Docs Api,Google Drive Api,我对谷歌文档有问题。我想使用OAuth 2.0检索您的所有文件。问题是,一旦您这样做,当尝试下载文件时,授权会给我以下错误: Exception in thread "main" java.lang.NullPointerException at GoogleBlack.readUrl(GoogleBlack.java:95) at GoogleBlack.getDocument(GoogleBlack.java:87) at GoogleBlack.go(GoogleBlack.java:18

我对谷歌文档有问题。我想使用OAuth 2.0检索您的所有文件。问题是,一旦您这样做,当尝试下载文件时,授权会给我以下错误:

Exception in thread "main" java.lang.NullPointerException
at GoogleBlack.readUrl(GoogleBlack.java:95)
at GoogleBlack.getDocument(GoogleBlack.java:87)
at GoogleBlack.go(GoogleBlack.java:187)
at GoogleBlack.main(GoogleBlack.java:222)
这是我使用的代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.GoogleAuthTokenFactory.UserToken;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.util.ServiceException;

public class GoogleBlack {

private static final String APPLICATION_NAME = "Homework";
public DocsService service;
public GoogleService spreadsheetsService;
static String CLIENT_ID = "***********";
static String CLIENT_SECRET = "**************";
static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
static List<String> SCOPES = Arrays.asList("https://docs.google.com/feeds");

public void getSpreadSheet(String id, String i, String ids, String type) throws Exception {
    UserToken docsToken = (UserToken) service.getAuthTokenFactory()
            .getAuthToken();
    UserToken spreadsheetsToken = (UserToken) spreadsheetsService
            .getAuthTokenFactory().getAuthToken();
    service.setUserToken(spreadsheetsToken.getValue());

    URL url = null;

    if(type.equals("doc"))
    {
        url = new URL("https://docs.google.com/feeds/download/documents/Export?id="+ ids);
    }

    else
    {
         url = new URL("https://docs.google.com/feeds/download/spreadsheets/Export?key="+ ids +"&exportFormat="+ type + "&gid=0");
         i += ".xls";
    }

    System.out.println("Spred = " + url.toString());

    readUrl1(url.toString(), i, type);
    service.setUserToken(docsToken.getValue());
}

public void readUrl1(String url, String i, String type) throws IOException, ServiceException
{
     MediaContent mc = new MediaContent();
     mc.setUri(url);
     MediaSource ms = service.getMedia(mc);   

     System.out.println("Name: "+i);
     BufferedInputStream bin = new BufferedInputStream(ms.getInputStream());
     OutputStream out = new FileOutputStream(i);
     BufferedOutputStream bout = new BufferedOutputStream(out);

     while (true) {
       int datum = bin.read();
       if (datum == -1)
         break;
       bout.write(datum);
     }
     bout.flush();
}

public void getDocument(String id, String i) throws Exception {
    URL url = new URL(id);
    readUrl(url,i);
}

public void readUrl(URL url, String i) throws Exception {
    MediaContent mc = new MediaContent();
    mc.setUri(url.toString());
    System.out.println("Url "+ url.toString());
    System.out.println("MC: " + mc.toString());
    MediaSource ms = service.getMedia(mc);   

    System.out.println("Name: "+i);
    BufferedInputStream bin = new BufferedInputStream(ms.getInputStream());
    OutputStream out = new FileOutputStream(i);
    BufferedOutputStream bout = new BufferedOutputStream(out);

    while (true) {
      int datum = bin.read();
      if (datum == -1)
        break;
      bout.write(datum);
    }
    bout.flush();






 //     FileOutputStream fout = null;
 //     fout = new FileOutputStream(i);
//      fout.write(cbuf.);
//        fout.close();

}

static Credential getCredentials() {
    HttpTransport transport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // Step 1: Authorize -->
    String authorizationUrl =
        new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = null;
    try {
        code = in.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // End of Step 1 <--

    // Step 2: Exchange -->
    GoogleTokenResponse response = null;
    try {
        response = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
            code, REDIRECT_URI).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // End of Step 2 <--

    // Build a new GoogleCredential instance and return it.
    return new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET)
        .setJsonFactory(jsonFactory).setTransport(transport).build()
        .setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());
  }

public void go() throws Exception {


    DocsService service = new DocsService(APPLICATION_NAME);
    service.setOAuth2Credentials(getCredentials());

    URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");

    DocumentListFeed feed = service.getFeed(feedUrl, DocumentListFeed.class);

    System.out.println("Feed " + feed.getEntries().size());

    for(int i = 0; i < feed.getEntries().size(); i++)
    {

            DocumentListEntry entry = feed.getEntries().get(i);
            if(entry.getType().equals("file"))
            {
                MediaContent mc1 = (MediaContent) entry.getContent();
                String UrlForDownload = mc1.getUri();
                System.out.println("Type is: " + entry.getType());
                System.out.println("File Name is: " + entry.getTitle().getPlainText());
                System.out.println("URL "+ UrlForDownload);

                getDocument(UrlForDownload, entry.getFilename());
            }

            else
            {
                MediaContent mc1 = (MediaContent) entry.getContent();
                String UrlForDownload = mc1.getUri();
                System.out.println("URL "+ UrlForDownload);
                System.out.println("Type is: " + entry.getType());
                System.out.println("File Name is: " + entry.getTitle().getPlainText());

                if(entry.getTitle().getPlainText().equals("Web Design 2011/2012 - Материали"))
                {
                    continue;
                }

                if(entry.getType().equals("spreadsheet"))
                {
                    String name = entry.getTitle().getPlainText().replaceAll(" ", "");
                    System.out.println("name: " + name);
                    getSpreadSheet(UrlForDownload, name, entry.getDocId(),"xls");
                }
                else
                {
                    String name = entry.getTitle().getPlainText().replaceAll(" ", "");
                    System.out.println("name: " + name);
                    getSpreadSheet(UrlForDownload, name, entry.getDocId(),"doc");
                }
            }
    }

}

public static void main(String[] args) throws Exception {

    new GoogleBlack().go();

}

}
import java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.BufferedReader;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.net.URL;
导入java.util.array;
导入java.util.List;
导入com.google.api.client.auth.oauth2.Credential;
导入com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
导入com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
导入com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
导入com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.http.javanet.NetHttpTransport;
导入com.google.api.client.json.jackson.JacksonFactory;
导入com.google.gdata.client.GoogleService;
导入com.google.gdata.client.GoogleAuthTokenFactory.UserToken;
导入com.google.gdata.client.docs.DocsService;
导入com.google.gdata.data.MediaContent;
导入com.google.gdata.data.docs.DocumentListEntry;
导入com.google.gdata.data.docs.DocumentListFeed;
导入com.google.gdata.data.media.MediaSource;
导入com.google.gdata.util.ServiceException;
公共类谷歌黑{
私有静态最终字符串应用程序\u NAME=“家庭作业”;
公共文件服务;
谷歌公共服务电子表格服务;
静态字符串客户端_ID=“**********”;
静态字符串CLIENT_SECRET=“****************”;
静态字符串重定向_URI=“urn:ietf:wg:oauth:2.0:oob”;
静态列表范围=数组。asList(“https://docs.google.com/feeds");
public void getSpreadSheet(字符串id、字符串i、字符串id、字符串类型)引发异常{
UserToken docsToken=(UserToken)服务。getAuthTokenFactory()
.getAuthToken();
UserToken电子表格斯托克=(UserToken)电子表格服务
.getAuthTokenFactory().getAuthToken();
service.setUserToken(spreadsheetsToken.getValue());
URL=null;
if(类型等于(“文件”))
{
url=新url(“https://docs.google.com/feeds/download/documents/Export?id=“+ids);
}
其他的
{
url=新url(“https://docs.google.com/feeds/download/spreadsheets/Export?key=“+ids+”&exportFormat=“+type+”&gid=0”);
i+=“.xls”;
}
System.out.println(“Spred=“+url.toString());
readUrl1(url.toString(),i,type);
setUserToken(docsToken.getValue());
}
public void readUrl1(字符串url、字符串i、字符串类型)引发IOException、ServiceException
{
MediaContent mc=新的MediaContent();
mc.setUri(url);
MediaSource ms=service.getMedia(mc);
System.out.println(“名称:+i”);
BufferedInputStream bin=新的BufferedInputStream(ms.getInputStream());
OutputStream out=新文件OutputStream(i);
BufferedOutputStream bout=新的BufferedOutputStream(输出);
while(true){
int datum=bin.read();
如果(基准==-1)
打破
书写(数据);
}
about.flush();
}
public void getDocument(字符串id,字符串i)引发异常{
URL=新的URL(id);
readUrl(url,i);
}
public void readUrl(URL URL,字符串i)引发异常{
MediaContent mc=新的MediaContent();
mc.setUri(url.toString());
System.out.println(“Url”+Url.toString());
System.out.println(“MC:+MC.toString());
MediaSource ms=service.getMedia(mc);
System.out.println(“名称:+i”);
BufferedInputStream bin=新的BufferedInputStream(ms.getInputStream());
OutputStream out=新文件OutputStream(i);
BufferedOutputStream bout=新的BufferedOutputStream(输出);
while(true){
int datum=bin.read();
如果(基准==-1)
打破
书写(数据);
}
about.flush();
//FileOutputStream fout=null;
//fout=新文件输出流(i);
//四写(cbuf);
//fout.close();
}
静态凭据getCredentials(){
HttpTransport传输=新的NetHttpTransport();
JacksonFactory jsonFactory=新JacksonFactory();
//步骤1:授权-->
字符串授权URL=
新的GoogleAuthorizationCodeRequestUrl(客户端ID、重定向URI、作用域);
//将用户指向或重定向到authorizationUrl。
System.out.println(“转到浏览器中的以下链接:”);
System.out.println(授权URL);
//从标准输入流中读取授权代码。
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“授权码是什么?”);
字符串代码=null;
试一试{
code=in.readLine();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//第1步结束
GoogleTokenResponse=null;
试一试{
响应=新的GoogleAuthorizationCodeTokenRequest(传输、jsonFactory、客户端ID、客户端机密、,
代码,重定向_URI).execute();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

//步骤2的末尾您从未初始化过“public DocsService service;”您的GoogleBlack类的成员,因此当您调用“service.getMedia(mc);”时,“您得到了一个NullPointerException。

谢谢您的回答。我找到了它,正准备编写,但谢谢