java.io.IOException:在GoogleCredential

时间:2016-04-03 12:51:03

标签: android google-api google-sheets

这是我的日志数据,有助于更轻松地检测错误

 java.io.IOException: No authentication challenges found
 at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java
 at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java
 at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java
 at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java
 at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java
 at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java
 at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java
 at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java
 at com.google.api.client.http.HttpRequest.execute(HttpRequest.java
 at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java
 at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java
 at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java
 at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java
 at com.cakewalk.licensekey.MySpreadsheet.<init>(MySpreadsheet.java
 at com.cakewalk.licensekey.MainActivity.onCreate(MainActivity.java
 at android.app.Activity.performCreate(Activity.java
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java
 at android.app.ActivityThread.access$600(ActivityThread.java
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java
 at android.os.Handler.dispatchMessage(Handler.java
 at android.os.Looper.loop(Looper.java
 at android.app.ActivityThread.main(ActivityThread.java
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java
 at dalvik.system.NativeStart.main(Native Method)

这是我的代码

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class OAuthIntegration{
    public static void main(String[] args) throws MalformedURLException,     GeneralSecurityException, IOException, ServiceException {
    URL SPREADSHEET_FEED_URL;
    SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    File p12 = new File("./key.p12");

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
    final List SCOPES = Arrays.asList(SCOPESArray);
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("cliend_ID")
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(p12)
            .build();

    SpreadsheetService service = new SpreadsheetService("Test");

    service.setOAuth2Credentials(credential);
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    if (spreadsheets.size() == 0) {
        System.out.println("No spreadsheets found.");
    }

     SpreadsheetEntry spreadsheet = null;
    for (int i = 0; i < spreadsheets.size(); i++) {
        if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {
            spreadsheet = spreadsheets.get(i);
            System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
            System.out.println("ID of SpreadSheet: " + i);
            }
        }

    }

}

添加行 credential.refreshToken(); 后出现此错误。我无法解决此错误的任何解决方案。我访问了this,但这篇文章对我没用。

0 个答案:

没有答案