0

Android Google plus , Login successfully on google account after that , facing Null pointer exception in following code , for getting google plus friend list.

GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
            httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute();

My Entire code are following :

public void setUp() throws IOException {

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // Go to the Google API Console, open your application's
    // credentials page, and copy the client ID and client secret.
    // Then paste them into the following code.

    String clientId = "Client_ID_for_Web_application";
    String clientSecret = "Client_secredt_for_Web_application";

    // Or your redirect URL for web based applications.
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    String scope = "https://www.googleapis.com/auth/contacts.readonly";


    // Step 1: Authorize -->

    String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId,
            redirectUrl,
            Arrays.asList(scope))
            .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 = in.readLine();
    // End of Step 1 <--

    // Step 2: Exchange -->
    GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
            httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute();
    // End of Step 2 <--

    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setClientSecrets(clientId, clientSecret)
            .build()
            .setFromTokenResponse(tokenResponse);

    People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
            .build();

    Log.e("peopel:", peopleService + "");

    ListConnectionsResponse response = peopleService.people().connections().list("people/me").execute();
    List<Person> connections = response.getConnections();

    Log.e("peope2:", connections + "");

    Person profile = peopleService.people().get("people/me").execute();

    Log.e("peope3:", profile + "");
}

Update

Error log are following :

  FATAL EXCEPTION: main
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tops.mygapp/com.example.tops.mygapp.MainActivity}: java.lang.NullPointerException
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                       at android.os.Looper.loop(Looper.java:137)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                    Caused by: java.lang.NullPointerException
                                                                       at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:213)
                                                                       at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
                                                                       at com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setCode(AuthorizationCodeTokenRequest.java:147)
                                                                       at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.setCode(GoogleAuthorizationCodeTokenRequest.java:147)
                                                                       at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.setCode(GoogleAuthorizationCodeTokenRequest.java:79)
                                                                       at com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.<init>(AuthorizationCodeTokenRequest.java:103)
                                                                       at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.<init>(GoogleAuthorizationCodeTokenRequest.java:111)
                                                                       at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.<init>(GoogleAuthorizationCodeTokenRequest.java:92)
                                                                       at com.example.tops.mygapp.MainActivity.setUp(MainActivity.java:272)
                                                                       at com.example.tops.mygapp.MainActivity.onCreate(MainActivity.java:77)
                                                                       at android.app.Activity.performCreate(Activity.java:5104)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                       at android.os.Looper.loop(Looper.java:137) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                       at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                       at dalvik.system.NativeStart.main(Native Method) 

Update 2

Exactly i am got null at

 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("What is the authorization code?");
 String code = in.readLine();

i am getting null value of code.

TopsAndy
  • 202
  • 2
  • 11

1 Answers1

0

As described in the manual: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtJava?hl=ru

you have to use this:

// Get authorization code from user.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String authorizationCode = null;
try {
  authorizationCode = in.readLine();
} catch (IOException ioe) {
  ioe.printStackTrace();
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • hello @Vyacheslav , it not help to solve my problem , my problem is same in exception , no getting any result due to null. – TopsAndy Sep 19 '16 at 13:28