Android - 使用OAuth 2.0&amp ;;从Google Plus获取Gender和BirthDate。火力地堡

时间:2018-04-04 08:50:08

标签: android firebase oauth-2.0 google-play-services google-plus

我只复制了“get ready to use the api”People API中的setUp函数,我按照列出的步骤进行了操作。 我添加了我的clientid和我的秘密,但我总是一样的错误。 我尝试从google plus帐户获取有关个人资料用户的信息(特别是性别和年龄),以便对用户进行分类。

与Firebase链接的Google身份验证功能完美,并为我提供姓名,邮件和Photourl。但是当我尝试使用OAuth 2.0连接谷歌加 - 谷歌时,它无法正常工作

- >我不明白为什么CODE总是为空

    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();

清单文件:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

Build.grale:

configurations {
all {
    exclude module: 'httpclient'
}
}
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v13:27.1.0'
implementation 'com.google.firebase:firebase-storage:12.0.1'
compile 'com.google.apis:google-api-services-people:v1-rev229-1.23.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'

implementation 'com.google.firebase:firebase-auth:12.0.1'
compile 'com.google.android.gms:play-services-location:12.0.1'
compile 'com.google.android.gms:play-services-auth:12.0.1'
compile 'com.google.android.gms:play-services-plus:12.0.1'
compile 'com.google.api-client:google-api-client:1.23.0'
//noinspection GradleCompatible
implementation 'com.google.firebase:firebase-database:12.0.1'
compile fileTree(include: ['*.jar'], dir: 'libs')

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ .SecondActivity20paste}: java.lang.NullPointerException
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
                                                                   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.teeen.user.teeen.SecondActivity20paste.setUp(SecondActivity20paste.java:561)
                                                                      at com.teeen.user.teeen.SecondActivity20paste.onCreate(SecondActivity20paste.java:176)
                                                                      at android.app.Activity.performCreate(Activity.java:6955)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
04-04 10:24:25.336 25756-25785/com. W/MessengerIpcClient: Timing out request: 1
04-04 10:24:25.338 25756-25784/com. E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE
04-04 10:25:25.375 25756-25784/com. E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE

请帮我解决。

0 个答案:

没有答案