2013-12-13 93 views
2

根據official announcement,Google+登錄功能現在支持沒有Google+個人資料的用戶。針對沒有Google+個人資料的用戶的Google+登錄

但是,當我從沒有Google+個人資料的帳戶訪問其中一個official examples時,它只能讓我選擇註冊Google+。

實現此功能的方式是什麼?我一直在檢查官方文檔,但我找不到任何有關此主題的參考。

+0

找到答案的運氣還不錯?我面臨着同樣的困難。 – Nebula

+0

是的。嘗試新的[遷移文檔](https://developers.google.com/+/api/auth-migration#sign-in)。您需要做的是使用配置文件範圍:「使用配置文件範圍添加Google+登錄按鈕提供了一種簡單,安全的方式來對用戶進行身份驗證,只訪問用戶的基本信息。用戶不會被提示創建Google+配置文件「。 – zugaldia

+0

是的,我試過了,但我仍然得到「創建配置文件」提示。你使用'GoogleApiClient'嗎?你可能會發佈一個關於你如何解決它的片段? – Nebula

回答

0

讓我們來做這個片段吧?

package com.example.googleplusauth; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.plus.Plus; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.IntentSender.SendIntentException; 
import android.os.Bundle; 

public class MainActivity extends Activity implements ConnectionCallbacks, 
     OnConnectionFailedListener { 

    /* Request code used to invoke sign in user interactions. */ 
    private static final int RC_SIGN_IN = 0; 

    /* Client used to interact with Google APIs. */ 
    private GoogleApiClient mGoogleApiClient; 

    /* 
    * A flag indicating that a PendingIntent is in progress and prevents us 
    * from starting further intents. 
    */ 
    private boolean mIntentInProgress; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Plus.API, null) 
       .addScope(Plus.SCOPE_PLUS_PROFILE) 
       .build(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
     if (requestCode == RC_SIGN_IN) { 
      mIntentInProgress = false; 

      if (!mGoogleApiClient.isConnecting()) { 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 

     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     if (!mIntentInProgress && result.hasResolution()) { 
      try { 
       mIntentInProgress = true; 
       startIntentSenderForResult(
         //result.getIntentSender(), 
         result.getResolution().getIntentSender(), 
         RC_SIGN_IN, null, 0, 0, 0); 

      } catch (SendIntentException e) { 
       // The intent was canceled before it was sent. Return to the 
       // default 
       // state and attempt to connect to get an updated 
       // ConnectionResult. 
       mIntentInProgress = false; 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     // We've resolved any connection errors. mGoogleApiClient can be used to 
     // access Google APIs on behalf of the user. 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     mGoogleApiClient.connect(); 
    } 

} 
相關問題