java.lang.NullPointerException: No authentication header information google oauthentication

南笙酒味 提交于 2019-12-11 05:39:25

问题


I am using the Google Data Protocol Client Libraries for authenticating my users. Here is the code I have so far:

        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();

        oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);

        oauthParameters.setOAuthConsumerSecret(SECRET_KEy);
        GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
        oauthParameters.setScope(SCOPE);
        oauthParameters.setOAuthVerifier(oauth_verifier);
        oauthParameters.setOAuthToken(oauth_token);
        oauthParameters.setOAuthTokenSecret(oauth_token_secret)
        String token = oauthHelper.getAccessToken(oauthParameters);
        System.out.println("here");
        ContactsService  client=new ContactsService ("myapp");
        client.setOAuthCredentials(oauthParameters, new hHmacSha1Signer());
        URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
        ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class);
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            ContactEntry entry = (ContactEntry) resultFeed.getEntries().get(i);
            System.out.println("|\t" + (i + 1) + ": "
                    + entry.getTitle().getPlainText());
        }

but on this line

ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class);

I am getting following exception:

    java.lang.NullPointerException: No authentication header information
        com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
        com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
        com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600)

com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
        com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
        com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
        com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
        com.google.gdata.client.Service.getFeed(Service.java:1135)
        com.google.gdata.client.Service.getFeed(Service.java:1077)
        com.google.gdata.client.GoogleService.getFeed(GoogleService.java:662)
        com.google.gdata.client.Service.query(Service.java:1237)
        com.google.gdata.client.Service.query(Service.java:1178)
        com.raisonne.oauth.CallBackAction.execute(CallBackAction.java:66)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    ....

I have already applied what has been mentioned at various forums but it hasn't fixed the problem. What should I do to fix this?


回答1:


I had the same problem, but after many changes, my code worked. Try add these two lines:

oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
client.setHeader("Authorization", "Bearer " + oauth_token);



回答2:


Set the scope:

oauthParameters.setScope("http://www.google.com/m8/feeds/contacts/default/full");


来源:https://stackoverflow.com/questions/8177156/java-lang-nullpointerexception-no-authentication-header-information-google-oaut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!