Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Yelp, OAuth, and Unity C# - 2015

Discussion in 'Scripting' started by OJ3D, Oct 30, 2015.

  1. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Yelp still uses OAuth 1.0 for authenticating - https://www.yelp.com/developers/api_console. I've taken a stab at utilizing the yelp API to pull information into a simple test I'm conducting. A search for food and city and pull in the JSON information. There's virtually no information on this process except for an attempt in 2013 by Steve Yaus - https://stevenyau.wordpress.com/2013/08/29/accessing-the-yelp-api-with-oauth-within-unity/
    Which utilizes an OauthBase.cs script to assist with key, and token signature generation. The problem is that the OauthBase group repository isn't properly maintained anymore -https://code.google.com/p/oauth/.

    Anyway, I went ahead and updated the original script to reflect what all the contributors indicated as bugs along with replacing the HTTP stuff with WWW class. To my knowledge this is the most up to date OauthBase script.:)

    Yelp has C# sample code - https://www.yelp.com/developers/documentation/v2/examples but the libraray references are .NET compatible and not compatible with Unity ( SimpleOauth.net in addition to newtonsoft.JSON.net - Can't find a single DLL or nuget package to come into Monodevelop)

    Anyway, I feel that I'm razor thin of getting it to work but I'm getting the follow Error - "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u..."

    It's clearly an issue with the the generation method, just can't figure it out/troubleshoot it. I would definitely appreciate some feedback, and please feel free to try it out yourself. Just bring the scripts into your project, slap the "Yelp_BasicSearch.cs" script to any gameobject in your scene, and run the scene. It'll print out results in the console and as a text file on your desktop . You may need to do a quick Yelp Dev signup to get keys/tokens to test it- https://www.yelp.com/developers/api_console

    P.S. - Found this unrelated Oauth example with fitbit and unity in unityscript though - http://technicalartistry.blogspot.be/2015/07/oauth2-unity-and-month-of-cursing.html?m=1
     

    Attached Files:

  2. yaustar_unity

    yaustar_unity

    Joined:
    Oct 30, 2015
    Posts:
    4
  3. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Gambit_for_hire - you're the bomb! Thanks again for the mega quick response and pointing me in the right direction. I'll let you know how it goes.
     
  4. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Gambit_for_hire you are a huge lifesaver, I swear man:eek:.
    Your code hit it right on the money$$$$. I used about 98% of it to get yelp to work.
    I really only made updates to 4 areas of your code.

    1. SearchTwitter_Coroutine Function - I changed the key value parameter pairs to {"term","food"},{"location",WWW.escapeurl("San Francisco, ca")} - clearly as a basic search run.

    2. CreateSignature Function -I reordered the urlParamsDictionary parameters Alphabetically based on section 3 of this article I found - https://blog.nraboy.com/2014/11/understanding-request-signing-oauth-1-0a-providers/

    3. CreateAuthorizationHeaderParameter Function - I reordered the Authorization header parameters to follow suite with the order indicated in the the Oauth.net spec - http://oauth.net/core/1.0a/#anchor12

    4. The Main URL link should be kept to the minimum - https://api.yelp.com/v2/search/

    Ran it, and got results.
    Thanks again man. I'll definitely pay it forward.
     
  5. yaustar_unity

    yaustar_unity

    Joined:
    Oct 30, 2015
    Posts:
    4
    Sorry for the late reply.. I forgot about that we started this conversation here.

    Glad to hear you got it working!

    If you could, can you submit a pull request/create a patch or even fork it please? I would be interested to grab your fixes.
     
  6. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Gambit_for_hire -Not a problem. I'm new to git and just signed up the other day. I'll create a repository and notify you when I get a chance. Do you have a git username so I can throw you props.
     
  7. yaustar_unity

    yaustar_unity

    Joined:
    Oct 30, 2015
    Posts:
    4
    The name I use for commits is 'Steven Yau' under the username 'gambitforhire' on BitBucket

    Cheers!
     
  8. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
  9. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
  10. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Chris- not to my knowledge. Thanks for the link. looks like a perfect option if they do implement 2.0
     
  11. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
    @OJ3D Thanks for sharing your snippet! Works awesome. I've got a search that brings me the best rated places in my area.

    This is my first time dabbling in all of the above (Oauth, WWW requests, JSON) What do you do with the results of the search? I'd like to filter out all the names and URLs of the restaurants in the results and pass them into clickable buttons.

    What are the words and methods that I should be researching to accomplish this?
     
  12. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Bridin - Glad it works.
    What you essentially receive is text that is structured in a JSON format. In order to read what you need, use SimpleJSON to parse the text file (Node) into a usable array - http://wiki.unity3d.com/index.php/SimpleJSON. Then create a loop routine to parse out what you need.
     
  13. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
  14. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    I don't know anything about the key stuff. I looked into that link, I didn't mess around with that stuff. Look at the SimpleJSON example scenes and the c# code. That's how I got things to work.

    it's something along the lines like this:
    //ReadYelpJSON is the www.text you recieved--i.e the JSON node
    Code (CSharp):
    1. JSONArray yelpitems = (JSONArray)ReadYelpJSON["businesses"];  //<--you'll see the businesses is the top most node in the JSON text
    2. int YelpArrayLength = yelpitems.Count; //for loop routines
    3.  
    4. //Initialize arrays of interest
    5. string [] ParsedMobileUrlText = new string[YelpArrayLength];
    6. string [] ParsedRatingText = new string[YelpArrayLength];
    7.  
    8. //Define the Arrays
    9. for (int i = 0; i < YelpArrayLength; i++)
    10.         {
    11.             ParsedMobileUrlText=ReadYelpJSON ["businesses"] ["mobile_url"];
    12.             ParsedRatingText=ReadYelpJSON ["businesses"] ["rating"];
    13. }
    14.  
    15. //Wallah!Usable arrays are now defined

    Also, if you're confused on the structure of a JSON node, use this tool (http://jsonviewer.stack.hu/) it's f-in! awesome. Just slap the text you receive, paste it and it'll show you the break down of the JSON node
     
    Last edited: Mar 2, 2016
    Kellyrayj likes this.
  15. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
    I'll give this a shot! Thank you!
     
  16. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
  17. rensutheart

    rensutheart

    Joined:
    Jan 28, 2016
    Posts:
    2
    Last edited: Jan 16, 2017
  18. sim1138

    sim1138

    Joined:
    Feb 26, 2016
    Posts:
    2