2009-08-31 152 views
17

當我打乒乓球與谷歌OpenID提供商,我不能讓一個友好的名稱/暱稱/用戶名(隨便你怎麼稱呼它)。Google使用OpenID的友好名稱?

我得到的東西惡劣看起來像以下:

www.google.com/accounts/o8/id?id=AItOawmtAnQvSXGhRTkAHZWyxi4L4EKa7xoTT1dk 

,而不是一件好事像

JohnDoe 

什麼是協議在一個不錯的友好的方式從谷歌獲得用戶的姓名就好說了, myopenid呢?

**我正在使用DotNetOpenAuth *

回答

13

你不能。 OP發佈的標識嚴格限於OP。 RP並沒有真正的發言權。現在,一些OP支持通過登錄提供屬性,例如暱稱,電子郵件地址等。Google對這些提供的支持非常有限,只提供電子郵件地址。

谷歌選擇不發出用戶可識別的標識,因爲它是一個信息泄露的風險。雅虎通過向用戶提供兩種用戶可以選擇的人性化和非人性化標識符來實現這兩種路線。 MyOpenID和其他OP通常只使用用戶友好的標識符,用戶在OP上註冊時選擇它們。

您可能需要特殊的情況下谷歌在你的RP挑一個更友好的字符串時,他們登錄到顯示給用戶,或者因爲谷歌是不是唯一一個這樣做,編寫代碼搞清楚當標識符不可讀時,向用戶顯示更友好的內容,以便他們知道他們已登錄(可能是他們的電子郵件地址或他們在您的網站上選擇的暱稱)。

注意:如果您選擇顯示一個比谷歌的問題更加友好的標識,你仍然必須使用官方聲明的標識符從谷歌爲你傳遞給FormsAuthentication.RedirectFromLogin和用戶名的用戶的正式用戶名在數據庫中查找。任何你放在一起的東西通常都會帶來安全風險。

+0

感謝您解決這個問題。我想用默認的友好標識符作爲用戶的電子郵件可以工作。你會碰巧知道哪些其他服務在友好名稱方面表現不佳? – MunkiPhD 2009-08-31 00:58:23

+0

雅虎是我所知道的唯一另一個,有時它只有這種行爲。 – 2009-08-31 04:02:31

+1

安德魯,我設法讓你的博客文章更深入,並發現與雅虎有關的文章。我必須再次閱讀才能完全理解它 - 但在我未知的試驗中,我甚至無法從雅虎收到一封電子郵件地址,當我使用雅虎登錄信息進行電子郵件轉寄時,這使我感到困惑 - 而且我的信息清楚地轉移了過來。 – MunkiPhD 2009-08-31 16:00:51

6

截至2012年,似乎Google OpenID endpoint支持通過屬性交換協議的姓氏和名字檢索。下面是使用Pyramid Web框架和Janrain的python-openid包一些示例Python代碼。羅伊回答

from openid.consumer import consumer 
from openid.extensions.ax import AttrInfo, FetchRequest, FetchResponse 
from openid.store.filestore import FileOpenIDStore 
from openid.store.sqlstore import PostgreSQLStore, MySQLStore, SQLiteStore 

AX_FIRSTNAME = 'http://axschema.org/namePerson/first' 
AX_LASTNAME = 'http://axschema.org/namePerson/last' 
AX_EMAIL = 'http://axschema.org/contact/email' 

@view_config(route_name='openID_start', permission=NO_PERMISSION_REQUIRED) 
def start(request): 
    'Start openID authentication process' 
    params = request.params 
    openIDURL = params.get('openIDURL') 
    if not openIDURL: 
     return HTTPResponse('Parameter expected: openIDURL') 
    openIDConsumer = get_consumer(request) 
    try: 
     openIDRequest = openIDConsumer.begin(openIDURL) 
    except consumer.DiscoveryFailure, error: 
     return HTTPResponse('Discovery failed: %s' % escape(error)) 
    else: 
     if not openIDRequest: 
      return HTTPResponse('Not an openID provider: %s' % escape(openIDURL)) 

     axRequest = FetchRequest() 
     axRequest.add(AttrInfo(AX_FIRSTNAME, required=True)) 
     axRequest.add(AttrInfo(AX_LASTNAME, required=True)) 
     axRequest.add(AttrInfo(AX_EMAIL, required=True)) 
     openIDRequest.addExtension(axRequest) 

     sourceURL = request.host_url 
     targetURL = request.route_url('openID_finish') 
     if openIDRequest.shouldSendRedirect(): 
      return HTTPFound(location=openIDRequest.redirectURL(sourceURL, targetURL)) 
     return HTTPResponse(openIDRequest.htmlMarkup(sourceURL, targetURL)) 

@view_config(route_name='openID_finish', permission=NO_PERMISSION_REQUIRED) 
def finish(request): 
    'Finish openID authentication process' 
    openIDConsumer = get_consumer(request) 
    targetURL = request.route_url('openID_finish') 
    openIDResponse = openIDConsumer.complete(request.params, targetURL) 
    html = openIDResponse.status + '<br>' 
    for key, value in openIDResponse.__dict__.iteritems(): 
     html += '%s: %s<br>' % (escape(key), escape(value)) 
    html += '<br>' 
    if consumer.SUCCESS == openIDResponse.status: 
     axResponse = FetchResponse.fromSuccessResponse(openIDResponse) 
     html += 'First name: %s<br>' % escape(axResponse.get(AX_FIRSTNAME)) 
     html += 'Last name: %s<br>' % escape(axResponse.get(AX_LASTNAME)) 
     html += 'Email: %s<br>' % escape(axResponse.get(AX_EMAIL)) 
    return HTTPResponse(html) 

def get_consumer(request): 
    try: 
     openIDStore = { 
      'sqlite': SQLiteStore, 
      'postgresql': PostgreSQLStore, 
      'mysql': MySQLStore, 
     }[db.bind.name](db.bind.raw_connection()) 
    except KeyError: 
     openIDStore = FileOpenIDStore('data/openIDs') 
    try: 
     openIDStore.createTables() 
    except: 
     pass 
    return consumer.Consumer(request.session, openIDStore) 
+0

像炸彈一樣工作。/namePerson/first和/ namePerson/last – Spider 2012-04-26 15:49:59

10

基地,我試圖讓使用DotNetOpenAuth同樣的要求和它工作得很好。 請求:

var req = openid.CreateRequest("https://www.google.com/accounts/o8/id"); 
var fetch = new FetchRequest(); 
fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email,true)); 
fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.First,true)); 
fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.Last,true)); 

req.AddExtension(fetch); 

注意:確保AttributeRequest構造函數的第二PARM設置爲true。

響應部分是直線前進。

var openid = new OpenIdRelyingParty(); 
var response = openid.GetResponse(); 
var fetch = response.GetExtension<FetchResponse>(); 
if (fetch != null) { 
IList<string> emailAddresses =fetch.Attributes[WellKnownAttributes.Contact.Email].Values; 
IList<string> firstNames = fetch.Attributes[WellKnownAttributes.Name.First].Values; 
IList<string> lastName = fetch.Attributes[WellKnownAttributes.Name.Last].Values; 
} 
+0

根據你使用的方式(我正在使用OWIN),你的項目中安裝的東西可能沒有WellKnownAttributes,你可能不想添加DotNetOpenAuth只是爲了這個。以下是WellKnownAttributes(以及其他OpenId庫中的其他地方)中的OpenId字符串:名:「given_name」,姓:「family_name」,中間名:「middle_name」,暱稱:「暱稱」 – 2016-05-22 17:02:39