IOS的foursquare oauth2 (foursquare oauth2 for IOS)


問題描述

IOS的foursquare oauth2 (foursquare oauth2 for IOS)

Im upgrading from v1 foursquare api to v2 which requires Oauth2. 

Is it correct that to use the web server flow as recommened I should direct the user to :  https://foursquare.com/oauth2/authenticate   ?client_id=YOUR_CLIENT_ID   &response_type=code   &redirect_uri=YOUR_REGISTERED_REDIRECT_URI

Once the user is authenticated  foursquare will redirect to :  https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE

Meaning I need to define an endpoint at https://YOUR_REGISTERED_REDIRECT_URI which will then make a request to 

https://foursquare.com/oauth2/access_token   ?client_id=YOUR_CLIENT_ID   &client_secret=YOUR_CLIENT_SECRET   &grant_type=authorization_code   &redirect_uri=YOUR_REGISTERED_REDIRECT_URI   &code=CODE

to get the actual token on my serverside. 

How does this flow get the token back to the mobile device for usage?

Thanks for the help. 


參考解法

方法 1:

This is just a guess my part, but here's a possible flow:

  1. open a uiwebview and send the user to https://foursquare.com/oauth2/authenticate ?client_id=YOUR_CLIENT_ID &response_type=code &redirect_uri=YOUR_REGISTERED_REDIRECT_URI
  2. after they accept, they will get redirected to https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE such as https://domainyouown.com/callback?code=asdfasdfasdfasdf
  3. have you callback page make a request to https://foursquare.com/oauth2/access_token ?client_id=YOUR_CLIENT_ID &client_secret=YOUR_CLIENT_SECRET &grant_type=authorization_code &redirect_uri=YOUR_REGISTERED_REDIRECT_URI &code=CODE
  4. get the json response (still in your callback page code), save it to your serverside db (if in use), and also display on the html of the page in a div with an id of 'oauth-token'.
  5. use UIWebView's - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script to get the value of the div and store it in your iphone settings

You may also want to check out https://github.com/nxtbgthng/OAuth2Client

方法 2:

if you're doing server-less flow (mobile app only) you'll do this route:

  1. Pop a UIWebview -> https://foursquare.com/oauth2/authenticate?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REGISTERED_REDIRECT_URI (make sure the redirect matches)
  2. Your redirected uri should point to your App's URL scheme (such as APPNAME://callbackuri). When the user finishes logging in, the UIWebview will call the redirected URI which includes the oauth token. The URI will call the method - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation in your app delegate.
  3. To get your access token from the url:
if ([url.absoluteString rangeOfString:@"access_token="].location != NSNotFound) 
        NSString *accessToken = [[url.absoluteString componentsSeparatedByString:@"="] lastObject];

Save that accessToken and make sure to include it in all Foursquare calls (parameter: oauth_token=ACCESSTOKEN)

(by imrank1shawnwallninjaneer)

參考文件

  1. foursquare oauth2 for IOS (CC BY-SA 3.0/4.0)

#oauth-2.0 #foursquare #grails #Mobile






相關問題

OAuth2 用戶映射和丟失我的 Cookie (OAuth2 User Mapping and Loosing my Cookies)

如何在打開 Facebook 登錄對話框之前告訴 iPhone 應用用戶會發生什麼 (How to tell an iphone app user what will happen before opening Facebook login dialogue)

帶有 spring-security 的 OAuth2 - 通過 HTTP 方法限制 REST 訪問 (OAuth2 with spring-security - limit REST access by HTTP method)

帶有Phonegap 2.3.0的Facebook oAuth沒有在成功url返回令牌作為url參數 (Facebook oAuth with Phonegap 2.3.0 not returning token as url param at success url)

Dwolla API Key、Client_id 和 Client_Secret 有什麼區別? (What is the different between Dwolla API Key, Client_id & Client_Secret?)

Linkedin 應用程序具有 OAuth 用戶令牌和 OAuth 用戶密鑰,它們會過期嗎? (Linkedin Application has OAuth User Token and OAuth User Secret, Do they Expire?)

OAuth2:保護非用戶資源 (OAuth2: Protecting non-user resources)

IOS的foursquare oauth2 (foursquare oauth2 for IOS)

Spring Boot 2 Oauth 如何實現隱式代碼流 (Spring Boot 2 Oauth how to implement Implicit Code Flow)

FusionAuth - 基於邀請的用戶登錄與社交登錄 (FusionAuth - Invite based user on-boarding with social logins)

為什麼每次發送請求時都刷新訪問令牌是個壞主意? (Why is it a bad idea to refresh access token every time when I sent request?)

如何配置必須使用“密碼”授權類型從授權服務器請求令牌的客戶端 Java 應用程序? (How do I configure a client Java application which must request a token from an authorization server using a 'password' grant type?)







留言討論