HttpTransport,NetHttpTransport,OAuthGetTemporaryToken无法解析为某种类型

时间:2015-10-21 13:16:45

标签: java api oauth

我尝试运行这些代码,但它会抛出错误:

  

HttpTransport无法解析为某种类型; NetHttpTransport不能   被解决为一种类型; OAuthHmacSigner无法解析为某种类型;     OAuthHmacSigner无法解析为某种类型;     OAuthGetTemporaryyToken无法解析为某种类型;     OAuthGetTemporaryToken无法解析为某个类型;     OAuthCredentialsResponse无法解析为某种类型;     OAuthAuthorizeTemporaryTokenUrl无法解析为某个类型;     OAuthAuthorizeTemporaryTokenUrl无法解析为某个类型;类型   mismatch:无法从String转换为boolean; OAuthGetAccessToken   不能解决一个类型; OAuthGetAccessToken无法解析   一种类型; OAuthCredentialsResponse无法解析为某种类型;     OAuthParameters无法解析为某个类型; OAuthParameters不能   被解决为一种类型; HttpRequestFactory无法解析为   类型; GenericUrl无法解析为某种类型; GenericUrl不能   解决了一个类型; HttpRequest无法解析为某种类型;     HttpResponse无法解析为某种类型;

我把代码放在这里:

package com.twitter.api;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthGetAccessToken;
import com.google.api.client.auth.oauth.OAuthGetTemporaryToken;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;



public class TwitterTest {
    private static final String CONSUMER_KEY="RM0WT9ZZXdjfILCPKyMwa6sD0";
    private static final String CONSUMER_SECRET="PxLhfLfch3GqK8IxbPqT8pzYgP86E8BmGxkHXvl0h8Jvw7hB3O";

    private static final String REQUEST_TOKEN_URL=
            "https://api.twitter.com/oauth/request_token";
    private static final String AUTHORIZE_URL=
            "https://api.twitter.com/oauth/authenticate";
    private static final String ACCESS_TOKEN_URL=
            "https://api.twitter.com/oauth/access_token";
    private static final String API_ENDPOINT_URL=
            "https://api.twitter.com/1.1/statuses/home_timeline.json";

    public static void main (String [] args) throws Exception{
        // HttpTransport will be used to handle the HTTP requests
        // This is part of the google-http library
        HttpTransport transport= new NetHttpTransport();

        // The OAuthHmasSigner will be used to create the oauth_signature
        // Using HMAC-SHAl as the oauth_signature_method
        // The signer needs the secret key to sign requests
        OAuthHmacSigner signer= new OAuthHmacSigner();
        signer.clientSharedSecret =CONSUMER_SECRET;

        // Step 1: Get a request token
        //-------------------------------
        // We need to provide our application key
        // We also need to provide an HTTP transport object
        // And the signer which will sign the request
        OAuthGetTemporaryToken requestToken = new OAuthGetTemporaryToken(REQUEST_TOKEN_URL);
        requestToken.consumerKey=CONSUMER_KEY;
        requestToken.transport=transport;
        requestToken.signer=signer;

        // Get back our request token
        OAuthCredentialsResponse requestTokenResponse= requestToken.execute();

        System.out.println("Request Token: ");
        System.out.println("- oauth_token    = " +requestTokenResponse.token);
        System.out.println("-oauth_token_secret = "+ requestTokenResponse.tokenSecret);

        // Update the signer to also include the request token
        signer.tokenSharedSecret= requestTokenResponse.tokenSecret;

        // Step 2: User grants access
        //----------------------------------------------

        // Construct an authorization URL using the temporary request token 
        OAuthAuthorizeTemporaryTokenUrl authorizeUrl=
                new OAuthAuthorizeTemporaryTokenUrl(AUTHORIZE_URL);
        authorizeUrl.temporaryToken = requestTokenResponse.token;

        // We ask the user to open this URL and grant access
        // Twitter includes an extra safety measure, asks the user to provide PIN
        String pin= null;
        System.out.println("Go to the following link:\n" + authorizeUrl.build());
        InputStreamReader converter = new InputStreamReader(System.in, "UTF-8");
        BufferedReader in = new BufferedReader(converter);
        while (pin == null){
            System.out.println("Enter the verification PIN provided by Twitter:");
            pin= in.readLine();     
        }

        // step 3: Request the access token the user has approved
        //---------------------------------------------------------------

        // Get the access token
        // We need to provide our application key
        // The signer, the transport objects
        // The temporary request token
        // And a verifier string ( the PIN number provided by Twitter)
        OAuthGetAccessToken accessToken = new OAuthGetAccessToken(ACCESS_TOKEN_URL);
        accessToken.consumerKey= CONSUMER_KEY;
        accessToken.signer= signer;
        accessToken.transport= transport;
        accessToken.temporaryToken=requestTokenResponse.token;
        accessToken.verifier= pin;

        // Get back our access token
        OAuthCredentialsResponse accessTokenResponse = accessToken.execute();

        System.out.println("Access Token: ");
        System.out.println("-oauth_token = "+ accessTokenResponse.token);
        System.out.println("-oauth_token_secret = "+ accessTokenResponse.tokenSecret);

        // Update the signer again
        // We now replace the temporary request token with the final access token 
        signer.tokenSharedSecret= accessTokenResponse.tokenSecret;

        // Set up OAuth parameters which can now be used in authenticated requests
        OAuthParameters parameters= new OAuthParameters();
        parameters.consumerKey=CONSUMER_KEY;
        parameters.token= accessTokenResponse.token;
        parameters.signer= signer;

        // OAuth steps finished, we can now start accessing the service
        //--------------------------------------------------------------

        HttpRequestFactory factory = transport.createRequestFactory(parameters);
        GenericUrl url = new GenericUrl(API_ENDPOINT_URL);
        HttpRequest req= factory.buildGetRequest(url);
        HttpResponse resp = req.execute();

        System.out.println(resp.getStatusCode());
        System.out.println(resp.parseAsString());
    }

}


And I already put all google jar.files to the build path in Eclipse.
Thank you for your help~~

0 个答案:

没有答案