Accessing the Yelp API with OAuth within Unity

I’m working on a side project at home which involved accessing the Yelp API with Unity which required the use of OAuth authentication and as the GitHub examples didn’t have any C# variations, I put one together myself with some help from Google.

The Yelp API is pretty straight forward despite the large number of parameters involved and the OAuth signature generation is handled by the outh project on code.google I found. As it only consists of a single source file, it makes it really simple to integrate in a project and use.

I had to make several changes to it as HttpUtility isn’t accessible in Unity and therefore I replaced all instances of HttpUtility.UrlEncode with WWW.EscapeURL which is part of Unity’s libraries and also added a bug fix that was purposed from the issues list from the project which can be found here.

A sample of the API call can be found after the jump.

using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using SimpleJSON; public class YelpAPI : MonoBehaviour { public string oauthConsumerKey; public string oauthConsumerSecret; public string oauthToken; public string oauthTokenSecret; public void QueryRestaurantsByLongitudeAndLatitude(double longitude, double latitude, Action callback) { StartCoroutine(QueryRestaurantsByLongitudeAndLatitude_Coroutine(longitude, latitude, callback)); } // Help formatting the request: http://stackoverflow.com/questions/6036934/accessing-yelps-oauth-1-0a-api-with-dotnetopenauth private IEnumerator QueryRestaurantsByLongitudeAndLatitude_Coroutine(double longitude, double latitude, Action callback) { string longitudeEscapedString = WWW.EscapeURL(longitude.ToString()); string latitudeEscapedString = WWW.EscapeURL(latitude.ToString()); string yelpSearchUrl = "http://api.yelp.com/v2/search?" + "ll=" + longitudeEscapedString + "," + latitudeEscapedString; string formattedUri = String.Format(System.Globalization.CultureInfo.InvariantCulture, yelpSearchUrl, ""); Uri urlUri = new Uri(formattedUri); string outNormalisedUrl = ""; string outNormalisedRequestParameters = ""; OAuth.OAuthBase oauthBase = new OAuth.OAuthBase(); string oauthSignature = oauthBase.GenerateSignature( urlUri, oauthConsumerKey, oauthConsumerSecret, oauthToken, oauthTokenSecret, "GET", oauthBase.GenerateTimeStamp(), oauthBase.GenerateNonce(), out outNormalisedUrl, out outNormalisedRequestParameters); string urlString = outNormalisedUrl + "?" + outNormalisedRequestParameters + "&oauth_signature=" + oauthSignature; WWW query = new WWW(urlString); yield return query; Debug.Log(query.text); } }
Advertisements
Tagged , , , ,

3 thoughts on “Accessing the Yelp API with OAuth within Unity

  1. OJ says:

    Steven,

    Glad I found your site. There is virtually no information regarding Yelp into Unity. The C# examples in Yelp’s documentation require .NET pacakgse ( SimpleOauth.net and newtonsoft.JSON.net).

    Anyway, I utilized what you had but changed it to do a Simple search by term and location.
    Also, the OauthBase.cs at code.google looks like it isn’t maintained anymore. So I updated the script to reflect what all the contributors had

    I’m have issues getting things to work. I could really use your help, please if you don’t mind look at what I made. I keep getting a signature generation error and can’t get pass that.

    I posted it on Unity’s Community Forum here – http://forum.unity3d.com/threads/yelp-and-oauth-c-2015.364816/ or do a search there for “Yelp and Oauth”

    I’d appreciate any feedback.
    Thanks again for sharing.
    Hope mine could help too.

    Thanks,
    OJ
    .

  2. OJ says:

    Steve you’re the bomb! Thanks for getting back to me. I’ll give the twitter version a swing and let you know how it goes.
    Thanks again man.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: