【问题标题】:Android Google plus安卓谷歌加
【发布时间】:2017-11-02 19:54:25
【问题描述】:

Android google plus,之后在google账号上登录成功,遇到下面代码中的空指针异常,获取google plus好友列表。

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

我的整个代码如下:

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 + "");
}

更新

错误日志如下:

  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) 

更新 2

正是我在

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

我得到的代码是空值。

【问题讨论】:

标签: android google-plus


【解决方案1】:

如手册中所述: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtJava?hl=ru

你必须使用这个:

// 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,这无助于解决我的问题,我的问题在异常中是相同的,由于null没有得到任何结果。
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 2011-05-11
  • 2019-08-12
  • 2016-03-28
相关资源
最近更新 更多