Open Platform-web enables third-party login on Renren network-web Renren
Application scenarios web applications enable third-party Logon Through Renren logon authorization. Operation Step 1 register as Renren Open Platform developer http://app.renren.com/developers/devinfo 2 prepare an accessible domain name, such as dev.foo.com 3 create web application, configure necessary information, this includes the root domain name and Icon information. 4. Obtain the application appID and appKey. The logon process is server-side. The process is as follows:
Process description1. Go to the logon Authorization page (Authorization code) on the server page. 2. Call back to get code 3 and replace accessToken, at the same time, uid and user information are also obtained. 4. Synchronizing user information and logging on to OAuth2.0 using Authorization code is more reliable and secure. For more information, see Renren Open Platform wiki: http://wiki.dev.renren.com/wiki/Authentication#.E6.9C.8D.E5.8A.A1.E7. AB .AF.E6.8E.88.E6.9D.83 case practice Local Development Environment prepare to modify the hosts file to map dev.foo.com to 127.0.0.1; local server to port 80 to start, in windows, port 80 may be occupied by the system process, the solution can refer to the http://www.cnblogs.com/littleatp/p/4414578.html local server after the startup of dev.foo.com domain name access, you can go to the frontend logon page through domain name verification during logon authorization.
<Html>
Function DescriptionWhen the page is opened, you can directly jump to the Renren logon authorization page. After successful authorization, the page will be called back to the redirect_uri server for processing the callback, and the page for synchronizing information authorization callbacks will be processed.
/*** Authorization callback *** @ param code * @ param error */public static void callback (String code) {if (! StringUtils. isEmpty (code) {error ("Authorization failed");} // exchange accesen en Based on the code, including user information //... string callbackUrl = RouteContext. getUrl ("openapi. renrens. callback ", Collections. EMPTY_MAP, true); RenrenToken token = RenApi. getTokenInfo (code, callbackUrl); if (token = null) {error ("Authorization failed: unable to get the connection system");} render (code, token );}
Data Object RenrenToken class
/*** Return token Data Object ** <pre> ** {* "token_type": "bearer", * "expires_in": 2595096, * "refresh_token ": "127021 | 0. KAS3b8doSitHk6RLDtitb2VY8PjktTRA. 229819774.1376381303243 ", *" user ": {*" id ": 229819700, *" name ":" miss two ", *" avatar ": [* {" type ": "avatar", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg" *}, * {"type": "tiny", * "url": "http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg "*}, * {"type": "main", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"}, * {"type": "large", * "url ": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg" *} *] *}, * "access_token": "127066 | bytes-229819774" * "scope": "read_user_feed read_user_album ", * </pre> ** @ author littleatp * @ createDate April 14, 2015 **/public class RenrenToken {public String token_type; public int expires_in; public String refresh_token; public String access_token; public String scope; public RenrenUser user ;}
RenrenUser class
/*** Renren user information ** <pre> * "user": {* "id": 229819700, * "name": "Miss II ", * "avatar": [* {"type": "avatar", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg" *}, * {"type": "tiny ", * "url": "http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg" *}, * {"type": "main", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"}, * {"type ": "large", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg" *} *] *}, * </pre> ** @ author littleatp * @ createDate July 15, April 14, 2015 **/public class RenrenUser {public long id; public String name; public List <RenrenAvatar> avatar; public String getAvatarUrl () {if (avatar = null | avatar. isEmpty () {return "";} return avatar. get (0 ). url;} public static class RenrenAvatar {public String type; public String url ;}}
RenApi function implementation
/*** Renren API ** <pre> * logon process: ** 1. Redirect from the frontend to Renren authorization (code method) * 2 get the authorize code through callback * 3 get the token and user information through code in exchange for access_token * 4 ** Reference document: * http://wiki.dev.renren.com/wiki/Authentication * </pre> *** @ author littleatp * @ createDate April 10, 2015 */public class RenApi {public static String apiKey = "xxx "; public static String secretKey = "xxx"; public static String baseUrl = "https://graph.renren.com/oaut H "; protected static final String URL_GET_TOKEN = baseUrl +"/token? Grant_type = authorization_code "+" & client_id = % s & client_secret = % s & code = % s & redirect_uri = % s "; protected static final long ACCESS_TIMEOUT = 15; protected static final String DEF_APP_TOKEN_EXPIRE = "3 h";/*** get token info ** <pre> * http://wiki.dev.renren.com/wiki/Authentication#.E5. AE .A2.E6.88.B7.E7. AB .AF.E6.8E.88.E6.9D.83 * returns token with user info ** call address: * https://graph.renren.com/oauth/token ** parameter * gra Nt_type: when Authorization Code is used as Access Grant, this value is fixed to "authorization_code"; * client_id: the API Key obtained when the application is registered in the Developer Center; * client_secret: secret Key obtained when registering an application in the Developer Center. The Secret Key is the confidential information of the application. Do not embed it into Code other than the server. * redirect_uri: it must be consistent with the "redirect_uri" passed when obtaining the Authorization code. * Code: authorization Code obtained in the above process. ** The returned result is as follows: * {* "token_type": "bearer", * "expires_in": 2595096, * "refresh_token": "127021 | 0. KAS3b8doSitHk6RLDtitb2VY8PjktTRA. 229819774.1376381303243 ", *" user ": {*" id ": 229819700, *" name ":" miss two ", *" avatar ": [* {" type ": "avatar", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg" *}, * {"type": "tiny", * "url": "http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg "*}, * {"type": "main", * "url": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"}, * {"type": "large", * "url ": "http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg" *} *] *}, * "access_token": "127066 | bytes-229819774" * "scope": "read_user_feed read_user_album", *} ** error returned: * {* "error": "invalid_grant", * "error_code": 20204 * "error_description": "Invalid authorization code: 9OCQp3IzRcwtSRPKOEUKiRRsz9SUNgdE "*} * http://wiki.dev.renren.com/wiki/%E9%94%99%E8%AF%AF%E5%93%8D%E5%BA%94 * </pre> ** @ param accesen en * @ return */public static RenrenToken getTokenInfo (String code, String callbackUrl) {if (StringUtils. isEmpty (code) {return null;} String url = String. format (URL_GET_TOKEN, apiKey, secretKey, code, callbackUrl); String resultString = DefaultHttp. get (url, ACCESS_TIMEOUT, GlobalConstants. UTF_8); Logger. debug ("[sso-renren] get token. use url '% S' ", url); RenrenToken token = JsonUtil. fromJson (resultString, RenrenToken. class); if (token = null | StringUtils. isEmpty (token. access_token) {Logger. debug ("[sso-renren] get token failed, with result of '% S'", resultString); return null;} Logger. debug ("[sso-renren] get token success, with result of '% S'", resultString); return token ;}}
What is CSRF? Can refer to the http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html Scenario A website connected to Renren open platform, but apikey and secretkey leaked out through the page; B website according to the same apikey and secretkey copy authorize request, get authorization code; website B directly jumps to the callback page of website A. Website A obtains user information and logs on to the website according to the authorization process. In this way, website B successfully realizes the function of impersonating requests to log on to website; the solution includes A state parameter when requesting authorization from the platform. It is recommended that this parameter be dynamically generated by website. When the Platform calls callback, this state parameter is returned. At this time, website A needs to perform this parameter during callback processing.
Verify. Therefore, website B cannot forge the state parameter, and thus cannot forge the login scenario. FAQ webpage jump prompt redirect_uri_mismatch is usually the root domain name in the application configuration is inconsistent with the current development server access address, resulting in the authorization to return error check returned error code http://wiki.dev.renren.com/wiki/%E9%94%99%E8%AF%AF%E5%93%8D%E5%BA%94 SDK usage for strong dependency on Renren Platform application, we recommend that you use sdks on the next platform, such as JavaSDK, which encapsulates a large number of api access and error handling details to improve development efficiency. Reference: http://wiki.dev.renren.com/wiki/V2/sdk/java_sdk