• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Foursquare native authentication makes it easier for your app's users to connect with Foursquare. Unlike web-based OAuth, native authentication re-uses the Foursquare app's user credentials, saving users the hassle of re-logging in to Foursquare within your app.

foursquare-ios-oauth

Foursquare native authentication makes it easier for your app's users to connect with Foursquare. Unlike web-based OAuth, native authentication re-uses the Foursquare app's user credentials, saving users the hassle of re-logging in to Foursquare within your app.

This repo includes a helper class (FSOAuth) that can be used as-is in your own app. It also includes a simple test application as an example of how to use the class.

Setting up FSOAuth with your app

  1. At http://foursquare.com/developers/apps enter the URL callback in the "Redirect URI(s)" field that you wish to Foursquare to use to return users to your app after authenticating. You can add multiple URIs in this field, separate them with commas. To support iOS 9, you will have to use a universal link (http or https). To support earlier iOS versions you will need to use a native custom URL scheme (e.g. yourappname://foursquare). If possible you should support both types of URL and register both as your redirect URIs.

  2. If you are supporting native URL schemes (iOS 8 and below), add your callback URL scheme to your app's Info.plist file (in the URL types field).

  3. If you are supporting universal links, you will need to set up your callback url appropriately. See Apple's documentation for more information.

  4. Add FSOAuth.{h,m} to your Xcode project. If you are using git for version control in your app, we recommend adding this repo as a submodule to yours to make it easier to get future updates. FSOAuth can be added to a project using CocoaPods.

Using FSOAuth

FSOAuth has three primary methods.

+ (FSOAuthStatusCode)authorizeUserUsingClientId:(NSString *)clientID
                        nativeURICallbackString:(NSString *)nativeURICallbackString
                     universalURICallbackString:(NSString *)universalURICallbackString
                           allowShowingAppStore:(BOOL)allowShowingAppStore
                      presentFromViewController:(UIViewController *)presentFromViewController;

Call this method with your app's client ID and callback string(s) to authorize a user with Foursquare. On iOS 9 or greater, a webview will be presented; on iOS 8 or lower, if a current version of the Foursquare app is installed, it will bounce the user out to that app and present them with an authorization dialog. After the user chooses Accept or Deny, your app will receive a callback at the url specified with the accessCode for the user attached.

The method will automatically select the correct callback string to use based on what version of iOS your app is running on. If your app runs on iOS 8 or lower you MUST support native URL schemes. If you are on iOS 9 or greater you may just use native scheme, but it is recommended you also provide a universal link callback if possible.

Note: Your callbacks MUST be added to the "Redirect URI(s)" field at http://foursquare.com/developers/apps or users will see an error message instead of the authorization prompt.

This method has five possible return values:

  • FSOAuthStatusSuccess The OAuth request was successfully initiated. The user has been bounced out to the Foursquare iOS app to approve or deny authorizing your app.
  • FSOAuthStatusErrorInvalidClientID You did not provide a valid client ID to the method.
  • FSOAuthStatusErrorInvalidCallback You did not provide a valid callback string that has been registered with the system.
  • FSOAuthStatusErrorFoursquareNotInstalled Foursquare is not installed on the user's iOS device.
  • FSOAuthStatusErrorFoursquareOAuthNotSupported The version of the Foursquare app installed on the user's iOS device is too old to support native auth.

If running on iOS 9 or above, you will not be able to get NotInstalled or NotSupported return values, as apps can no longer freely check what URL schemes are registered with the system. Instead, if an appropriate version of the Foursquare app is not installed, the web version of the Foursquare OAuth page will open in Safari.

If the allowShowingAppStore param is set to YES, then when returning FSOAuthStatusErrorFoursquareNotInstalled or FSOAuthStatusErrorFoursquareOAuthNotSupported, this method will present the user with the Foursquare app's page on the App Store so that the may easily install or update the app (by bouncing them out to the App Store app, or by presenting a modal StoreKit sheet if running on iOS 6+ and compiled with at least the iOS 6 SDK). If you pass NO, you should manually handle these two return values appropriately. This parameter has no effect if running on iOS 9 or later as there is no way to detect if Foursquare is installed when using universal links.

+ (nullable NSString *)accessCodeForFSOAuthURL:(NSURL *)url 
+                                        error:(nullable FSOAuthErrorCode *)errorCode;

Call this method when you receive the callback from Foursquare, passing in the NSURL object you received. It will parse out the access code and error code (if any) from the URL's parameters and return them to you.

NOTE: Your app may receive access code callbacks that it did not initiate (e.g. by a user initially choosing to connect to your app via a screen in the Foursquare app). Therefore you should not rely on your app being in any particular state when your callback is received.

The possible error code values are:

  • FSOAuthErrorNone There was no error and the access code was read successfully.
  • FSOAuthErrorUnknown An unrecognized error string was returned from the Foursquare server or the URL could not be parsed properly
  • FSOAuthErrorInvalidRequest / FSOAuthErrorInvalidClient / FSOAuthErrorInvalidGrant / FSOAuthErrorUnauthorizedClient / FSOAuthErrorUnsupportedGrantType - These enumeration values correspond to the OAuth error codes listed at http://tools.ietf.org/html/rfc6749#section-5.2.
+ (void)requestAccessTokenForCode:(NSString *)accessCode
                         clientId:(NSString *)clientID
                callbackURIString:(NSString *)callbackURIString
                     clientSecret:(NSString *)clientSecret
                  completionBlock:(FSTokenRequestCompletionBlock)completionBlock;

This method will initiate an asynchronous network request to Foursquare to convert a user's access code into an auth token.

WARNING: For security reasons, it is recommended that you not use this method if possible. You should pass the returned accessCode to your own server and have it contact the Foursquare server to convert the code to an access token instead of including your client secret in your app's binary. However, this helper method is provided for you to use if this is not possible for your app.

Call this method with the access code returned to you by +accessCodeForFSOAuthURL:error: along with your app's Foursquare client ID, callback string, and client secret. The callback URI must be the same one that was used to generate the access code.

When the network request completes, your completion block will be called. The block has the following signature:

typedef void (^FSTokenRequestCompletionBlock)(NSString *authToken, BOOL requestCompleted, FSOAuthErrorCode errorCode);

authToken will be set to the Foursquare OAuth token for the user if the request succeeded.

errorCode is an error code from the Foursquare server. It has the same possible values as the errorCode from +accessCodeForFSOAuthURL:error:. (See above.)

requestCompleted will be YES if the network request actually completed properly or NO if it did not. If this is NO, the values of authToken and errorCode should be ignored. If NO, you may want to re-try the request again after checking that the user has a valid internet connection. This could also indicate a temporary problem with the Foursquare servers.

Using the example application

The example application can be used as a simple reference for how to use the FSOAuth in your class, as well as a basic test to make sure your client id and secret is working properly.

The app will present you with fields to enter your client id, client secret, and callback URL. It has two buttons. The first initates the fast app switch to the Foursquare app and gets the access code. The second converts a received access code to a token by contacting the Foursquare servers. This will only work after first successfully receiving an access code from the initial Foursquare fast app switch.

The app itself uses "fsoauthexample" as its schema. If you want to be redirected back to it after the fast app switch (instead of to your own app) you will need to add an fsoauthexample redirect URI to your app's settings on foursquare.com (or change the Info.plist and FSViewController.m's -handleURL: method to match one of your existing redirect schemas). This is necessary for the code �→ token conversion functionality to work.

You should hard code all these values in your own application. Your client secret should be stored only on your own server, if possible, and not included in the app at all.

The example application contains code showing you how to set up universal link callback handling. However as it has no matching https url that will launch it, you cannot actually use a universal link callback to go back to it out of the box.

More Information

See https://developer.foursquare.com for more information on how to use the Foursquare API. foursquare-ios-oauth is currently maintained by Sam Grossberg (@samgro).

More Repositories

1

rogue

MOVED - The project is still under development but this page is deprecated.
Scala
489
star
2

twofishes

MOVED - The project is still under development but this page is deprecated.
Scala
433
star
3

FSNetworking

foursquare iOS networking library
Objective-C
384
star
4

fsqio

A monorepo that holds all of Foursquare's opensource projects
Scala
254
star
5

quattroshapes

Makefile
231
star
6

FSQCollectionViewAlignedLayout

FSQCollectionViewAlignedLayout is a generic collection view layout designed to be very flexible and configurable. It's goal is to save its users from having to write their own custom layout classes every time UICollectionViewFlowLayout is not appropriate for their view.
Objective-C
176
star
7

fongo

faked out in-memory mongo for java
Java
150
star
8

foursquare-android-oauth

Foursquare native authentication makes it easier for your app's users to connect with Foursquare. Unlike web-based OAuth, native authentication re-uses the Foursquare app's user credentials, saving users the hassle of re-logging in to Foursquare within your app.
Java
134
star
9

foursquare-palmpre

A webOS app (Mojo Framework)
JavaScript
105
star
10

slashem

A rogue-like DSL for querying SOLR
Scala
103
star
11

FSQLocationBroker

A centralized location manager for your app.
Objective-C
94
star
12

oozie-web

A more pretty, more usable web dashboard for Apache Oozie, written in Scala.
JavaScript
74
star
13

foursquare-fhttp

MOVED - The project is still under development but this page is deprecated.
Scala
44
star
14

FSQCellManifest

A UITableView and UICollectionView delegate and datasource that provides a simpler unified interface for describing your sections and cells.
Objective-C
43
star
15

quiver

An HFile-backed Key-Value Server
Go
42
star
16

hackathon

foursquare hackathonsâ„¢
40
star
17

spindle

MOVED - The project is still under development but this page is deprecated.
Scala
39
star
18

mongo-hdfs-export

Scala
31
star
19

foursquare-app-framework

Framework for building Connected Apps
Python
31
star
20

react-foursquare

Foursquare Library for React
JavaScript
25
star
21

es-scorer-plugin

Plugin to do our scoring in ES
Scala
24
star
22

sites-to-markdown

convert google sites html to markdown
Java
23
star
23

FSQRoutes

URL routing framework for iOS
Objective-C
21
star
24

fsq-studio-sdk-examples

Foursquare Studio is a platform to visualize, unify, enrich, and analyze spatial data on a planetary scale.
Jupyter Notebook
19
star
25

qgis-plugin

Foursquare Studio plugin for QGIS
Python
19
star
26

datasource-plugin-clouderamanager

Cloudera Manager datasource for Grafana 3.x
JavaScript
19
star
27

twitter-util-async

scala-async support for twitter util library
Scala
15
star
28

Place-API-Postman-Collection

Postman collection that contains almost all the sample Foursquare Places API calls.
14
star
29

foursquair

An Adobe AIR desktop client for foursquare
ActionScript
14
star
30

placepicker-sdk-sample

An SDK to help developers add a place picker to their app and also quickly access the Foursquare place that their user is at.
Java
11
star
31

wait

wait gem: executes a block until there's a result
Ruby
10
star
32

hoursparser.js

dumb but useful hours extractor from free-text entry
JavaScript
9
star
33

h3-presto

Presto bindings for H3, a hierarchical hexagonal geospatial indexing system
Java
8
star
34

gitshed

git versioning of large binary files outside the repo.
Python
8
star
35

shapefile-geo

Java
8
star
36

pilgrim-sdk-react-native

React native wrapper for the Pilgrim SDK
Java
7
star
37

fsgo

Reusable libraries for building Go services
Go
7
star
38

FSQMessageForwarder

An Obj-C message forwarder class, for when you don't have access to the source of the sending object.
Objective-C
6
star
39

merchant-app

JavaScript
5
star
40

source_code_analysis

Utilities to analyze, lint and rewrite source code in various languages.
Python
5
star
41

gohfile

5
star
42

exceptionator

MOVED - The project is still under development but this page is deprecated.
JavaScript
5
star
43

foursquare-places

framework agnostic wrapper for foursquare's APIs
JavaScript
5
star
44

android-map-utils

A collection of 3rd party map utility classes
4
star
45

cc-shapefiles

Scala
3
star
46

pilgrim-unity-package

Unity package which enables easy integration with Pilgrim SDK
Objective-C
3
star
47

foursquareapi-csharp

C#
3
star
48

foursquare.github.io

Foursquare open source portal
HTML
2
star
49

movementsdk-ios-spm

Movement SDK for iOS - Swift Package Manager
Swift
2
star
50

foursquare-places-api-samples

Developer Examples for using Foursquare products
HTML
2
star
51

simple-macros

MOVED - The project is still under development but this page is deprecated.
Scala
2
star
52

FSQComponents

Objective-C
2
star
53

RNPilgrimSample

Pilgrim sample app using React Native
Java
2
star
54

json-traverser

Scala
1
star
55

hackmidwest

This repo contains everything developers need to get started at Hack Midwest!
1
star
56

MovementSdk-CocoaPods-Beta

Private CocoaPods Spec repo for the Movement SDK
Ruby
1
star
57

finagle-dual

Support thrift and HTTP on same port with Finagle
Scala
1
star
58

movement-sdk-react-native

React native wrapper for the Movement SDK
Objective-C
1
star
59

pilgrimsdk-adobe-extension

The pilgrim adobe extension
Kotlin
1
star
60

public-model-resources

Jupyter Notebook
1
star
61

mobbing-interview-python

Used by the Security & Quality Team for interviews
1
star
62

RNMovementSample

Movement SDK sample app using React Native
Java
1
star
63

Pilgrim-CocoaPods-Beta

Public cocoapods spec repo for Pilgrim SDK beta builds
Ruby
1
star
64

pilgrim-ios-spm

Pilgrim SDK for iOS - Swift Package Manager
Swift
1
star
65

alertmon

Foursquare's homegrown production alerting platform
Python
1
star
66

commons-old

Temporary duplicate of foursquare/commons (a fork of twitter/commons) while we restructure things.
Java
1
star