2011-08-30 54 views
1

我的應用程序允許用戶使用Google或Yahoo的OpenID登錄。還有一個功能允許用戶上傳到YouTube。部分用戶通過YouTube上下文抵達,目的是創建內容並上傳到YouTube。這些用戶需要授權我的應用程序通過OpenID訪問其Google帳戶的地址,以及通過OAuth訪問其YouTube帳戶。我希望通過單個授權點擊來實現這一點。使用openid4java的YouTube的OpenID + OAuth

我已經看到這裏完成:http://www.youtube.com/create/Xtranormal。從這個程序發送到谷歌OpenID端點請求爲:

https://accounts.google.com/o/openid2/auth? 
openid.ns=http://specs.openid.net/auth/2.0& 
openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select& 
openid.identity=http://specs.openid.net/auth/2.0/identifier_select& 
openid.return_to=http://www.xtranormal.com/social/openid/complete/?next%3Dhttp%253A%252F%252Fyoutube.xtranormal.com%252Fytmm%252Fauth_popup_done%252F%26janrain_nonce%3D2011-08-29T16%253A35%253A53ZW0VqRw& 
openid.assoc_handle=AOQobUcMlV0Hmk431QROK27UegIYqYffiPeCuZ8gsB2x5ULYP0FXuoDZ& 
openid.ax.mode=fetch_request& 
openid.ax.required=ext0,ext1,ext2& 
openid.ax.type.ext0=http://axschema.org/namePerson/first& 
openid.ax.type.ext1=http://axschema.org/namePerson/last& 
openid.ax.type.ext2=http://axschema.org/contact/email& 
openid.mode=checkid_setup& 
openid.ns.ax=http://openid.net/srv/ax/1.0& 
openid.ns.oauth=http://specs.openid.net/extensions/oauth/1.0& 
openid.ns.sreg=http://openid.net/extensions/sreg/1.1& 
openid.oauth.consumer=www.xtranormal.com& 
openid.oauth.scope=http://gdata.youtube.com/& 
openid.realm=http://www.xtranormal.com/& 
openid.sreg.optional=postcode,country,nickname,email,fullname 

上的所有應用程序(效果很好)其他OpenID的支持下,與OpenID4Java寫的。我嘗試通過實施This answer中的提示來創建類似的請求,但是,我不能讓Google彈出窗口向我詢問YouTube,它只會要求輸入電子郵件地址。

我加入此消息擴展添加從答案中的參數:

public class OAuthHybridRequest implements MessageExtension{ 
    public static String SCOPE_YOUTUBE = "http://gdata.youtube.com/"; 
    ParameterList parameters; 
    public OAuthHybridRequest(String scope){ 
     parameters = new ParameterList(); 
     parameters.set(new Parameter("consumer", DeploymentProperties.getDeploymentProperty("OAUTH_CONSUMER_KEY"))); 
     parameters.set(new Parameter("scope", scope)); 
    } 
    public ParameterList getParameters() { 
     return parameters; 
    } 
    public String getTypeUri() { 
     return "http://specs.openid.net/extensions/oauth/1.0"; 
    } 
    ... 
} 

這讓我的要求是這樣的:

https://accounts.google.com/o/openid2/auth? 
openid.ns=http://specs.openid.net/auth/2.0& 
openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select& 
openid.identity=http://specs.openid.net/auth/2.0/identifier_select& 
openid.return_to=http://martin.test.example.no/socialdelegation/hybrid/youtube/sso/auth?is_callback%3Dtrue%26requestedURL%3D%252Fmovieeditor%252Fscripts%252Fpopupcloser.jsp& 
openid.realm=http://martin.test.example.no& 
openid.assoc_handle=AOQobUcMkuyp1pVZjpF-b8dVqTfB6Y6IyOZxihsk-XD1DOq0xv06lrlPgaJEF-ITUCdJiXPi& 
openid.mode=checkid_setup& 
openid.ns.ext1=http://specs.openid.net/extensions/oauth/1.0& 
openid.ext1.consumer=test.example.no& 
openid.ext1.scope=http://gdata.youtube.com& 
openid.ns.sreg=http://openid.net/sreg/1.0& 
openid.sreg.required=fullname,nickname,email& 
openid.ns.ext3=http://openid.net/srv/ax/1.0& 
openid.ext3.mode=fetch_request& 
openid.ext3.type.email=http://axschema.org/contact/email& 
openid.ext3.type.firstName=http://axschema.org/namePerson/first& 
openid.ext3.type.lastName=http://axschema.org/namePerson/last& 
openid.ext3.type.userName=http://axschema.org/namePerson/friendly& 
openid.ext3.type.gender=http://axschema.org/person/gender& 
openid.ext3.type.fullName=http://axschema.org/namePerson& 
openid.ext3.required=email,firstName,lastName,userName,gender,fullName 

缺少什麼我在這裏?

回答

2

下載oauth ext for openid4java zip文件from here (comment 8)並將類添加到您的項目中。然後:

// enable oauth ext for openid4java (do once) 
Message.addExtensionFactory(OAuthMessage.class); 

// add oauth extension to open-id request 
AuthRequest authReq = ...; 
OAuthRequest oauthRequest = OAuthRequest.createOAuthRequest(); 
oauthRequest.setScopes("oauth scope"); 
oauthRequest.setConsumer("oauth consumer key"); 
authReq.addExtension(oauthRequest); 

// extract oauth request token from open-id response 
AuthSuccess authSuccess = ...; 
if (authSuccess.hasExtension(OAuthMessage.OPENID_NS_OAUTH)) { 
    OAuthResponse oauthRes = (OAuthResponse) authSuccess 
     .getExtension(OAuthMessage.OPENID_NS_OAUTH); 
    // use this request token (without secret and verifier) and your oauth lib 
    // to get oauth access token 
    String oauthRequestToken = oauthRes.getRequestToken(); 
} 
+0

謝謝你,這是偉大的東西,我想我看到了它越快。然而,實施之後,谷歌彈出仍然不問我的YouTube,僅僅是電子郵件地址。我已經嘗試將領域改爲http://*.example.com,也無濟於事。你有什麼想法,爲什麼這可能是? – juell

+0

@OhHiThere你有沒有嘗試過https://gdata.youtube.com作爲範圍(https而不是http)? (順便說一句,當你使用多個作用域時,你應該在作用域之間使用空間)。 –

+0

或者也許oauth ext並未真正添加到openid請求中。使用Firefox「Tamper Data」插件來驗證您的重定向並確保已添加oauth參數。 –