Android使用Google API设置凭据

时间:2019-05-01 09:39:42

标签: android oauth credentials

我想创建一个Android应用程序并使用Google People Api。我遵循了本教程的所有步骤:https://developers.google.com/people/v1/getting-started

因此,我在Google Api Console中创建了一个新项目,获取了Web应用程序的客户端ID和客户端密码,并设置了同意屏幕oauth。最后,我尝试运行可以在上面的链接中看到的google代码,但是在创建“ GoogleTokenResponse”时出现异常。

我发布了异常,即使我觉得它没有用:

Process: simone.biliato.testpeopleapi, PID: 13067
java.lang.RuntimeException: Unable to start activity ComponentInfo{simone.biliato.testpeopleapi/simone.biliato.testpeopleapi.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
    at android.app.ActivityThread.access$900(ActivityThread.java:172)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5832)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: java.lang.NullPointerException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
    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 simone.biliato.testpeopleapi.MainActivity.setUp(MainActivity.java:63)
    at simone.biliato.testpeopleapi.MainActivity.onCreate(MainActivity.java:27)
    at android.app.Activity.performCreate(Activity.java:6221)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 

有人有相同的东西吗?有什么可行的例子吗? 我需要使用“ Google Button登录”吗?

1 个答案:

答案 0 :(得分:0)

我认为您可以尝试使用以下示例: https://github.com/sdivakarrajesh/Google-Drive-Java-API

它用于Google云端硬盘Api,但您可以使用代码进行许可,然后更改范围。