コード例 #1
0
    private void RequestIPGeoLocationInfo()
    {
        _outputText = "";

        // Simply pass in the URL for the API endpoint and wait for the response...
        string url = "http://freegeoip.net/json/";

        APIBuddy.GetInstance().SendGetRequest <GeoResponseData>(url, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
            if (success)
            {
                // APIBuddy handles populating GeoResponseData for us, based on the fields we set up in it up above.  All we need to do now is
                // cast responseObject to the GeoResponseData type and then read our data!
                GeoResponseData geoResponseData = (GeoResponseData)responseObject;

                _outputText  = "IP address: " + geoResponseData.ip;
                _outputText += "\nCountry: " + geoResponseData.country_name;
                _outputText += "\nRegion: " + geoResponseData.region_name;
                _outputText += "\nCity: " + geoResponseData.city;
            }
            else
            {
                _outputText = "Oops!  Couldn't retrieve IP geolocation info.\n" + errorMessage;
            }
        });
    }
コード例 #2
0
    void login()

    {
        const string LSurl = "https://app.lifescope.io/auth?";

        const string clientID      = "b47e68020f898a74";
        const string clientSecret  = "52570d6eccc4139f315079e0f5f030a7202b42fc7f90ae02c49fac3bdf0bb762";
        const string redirect_uri  = "";
        const string scope         = "";
        const string response_type = "code";

        const string LogInurl = LSurl + "client_id=" + clientID + "&redirect_uri=" + redirect_uri +
                                "&scope=" + scope + "&response_type=" + response_type;

        APIBuddy.GetInstance().SendGetRequest <LSAuthResponseData>(LogInurl, 30.0f, (bool success, string errorMsg, int statusCode, object responseObject) => {
            if (success)
            {
                LSAuthResponseData lsAuthResponseData = (LSAuthResponseData)responseObject;
            }
            else
            {
                Console.WriteLine(errorMsg);
            }
        });
    }
コード例 #3
0
    private void RequestTeamFortressNews()
    {
        _outputText = "";

        // Simply pass in the URL for the API endpoint and wait for the response...
        string url = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=440&count=3&maxlength=300&format=json";

        APIBuddy.GetInstance().SendGetRequest <SteamResponseData>(url, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
            if (success)
            {
                // APIBuddy handles populating SteamResponseData for us, based on the fields we set up in it up above.  All we need to do now is
                // cast responseObject to the SteamResponseData type and then read our data!
                SteamResponseData steamResponseData = (SteamResponseData)responseObject;

                _outputText = "";

                if (steamResponseData.appnews != null && steamResponseData.appnews.newsitems != null)
                {
                    foreach (SteamResponseData.NewsItem newsItem in steamResponseData.appnews.newsitems)
                    {
                        _outputText += "==== " + newsItem.title + " ====";
                        _outputText += "\n(" + newsItem.url + ")";
                        _outputText += "\n" + newsItem.contents;
                        _outputText += "\n\n";
                    }
                }
            }
            else
            {
                _outputText = "Oops!  Couldn't Team Fortress news.  This demo won't work if you are targeting WebPlayer or have the Editor set to emulate WebPlayer security because api.steampowered.com does not have a crossdomain.xml file.  The IP to Location demo should still work because freegeoip.net does have a crossdomain.xml file that permits access from anywhere.\n" + errorMessage;
            }
        });
    }
コード例 #4
0
    private void GetPerson()
    {
        _outputText         = "";
        _isRequestingPerson = true;

        if (string.IsNullOrEmpty(oauthClientId))
        {
            _outputText         = "Oops!  Be sure to set the OAuth Client ID provided to you by LifeScope.  Check the Inspector panel on the LifeScope object and make sure the OAuth Client Id field is not empty.";
            _isRequestingPerson = false;
            return;
        }

        // This is where we tell the OAuth server to start listening on whatever port number we've specified, and to send us the auth token when authentication is complete.
        string serverURL = _oauthCallbackServer.StartListening(portNumber, (string authToken) => {
            // At this point, authentication has completed and we've received the auth token.  Now we'll construct a URL for Instagram's API with that auth token and request some photos.
            string recentMediaURL = "https://api.instagram.com/v1/users/self/media/recent/?access_token=" + Uri.EscapeDataString(authToken);
            APIBuddy.GetInstance().SendGetRequest <LifeScopePersonOneDataResponse>(recentMediaURL, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
                if (success)
                {
                    int numPhotosRetrieved = 0;

                    // APIBuddy handles populating LifeScopePersonOneDataResponse for us, based on the fields we set up in it up above.  All we need to do now is
                    // cast responseObject to the LifeScopePersonOneDataResponse type and then read our data!
                    LifeScopePersonOneDataResponse LifeScopeResponse = (LifeScopePersonOneDataResponse)responseObject;

                    _outputText += "Username: "******"\nAvatar URL: " + LifeScopeResponse.data.personOne.avatar_url;
                    _isRequestingPerson = false;
                }
                else
                {
                    _outputText         = "Unable to retrieve LifeScope Person.\n" + errorMessage;
                    _isRequestingPerson = false;
                }
            });
        });

        // Now that we have the server URL, we can pass it as the redirect URI to Instagram's OAuth API.  When authentication is complete, Instagram will
        // send the user to the redirect URI with the auth token appended, and the server we started up above will capture that auth token so we can make
        // a request to the Instagram API for recent photos.
        string userAuthenticationURL = string.Format("https://app.lifescope.io/auth/?client_id={0}&redirect_uri={1}&response_type=token&state=12345678", Uri.EscapeDataString(oauthClientId), Uri.EscapeDataString(serverURL));

        Application.OpenURL(userAuthenticationURL);

        // If you're wanting to do OAuth 2.0 on iOS or Android, you won't be able to use the _oauthCallbackServer approach above.  Instead, you'll need to
        // register a custom URL scheme:
        // https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html
        // https://developer.android.com/training/basics/intents/filters.html
        //
        // Then for example, if your custom url scheme is myapp://, you'll tell the OAuth provider (Instagram in this example) that your client redirect URL
        // is something along the lines of myapp://oauth/.  Then after Instagram authenticates the user, it will redirect the user to myapp://oauth/#access_token=abcd1234
        // From there, you will need to read the access token in native Objective C, Swift, or Java (depending on your platform).  Then you can pass that data
        // back into your Unity app.  See this Stack Overflow post for an example of how data can be passed from iOS or Android to Unity:
        // http://stackoverflow.com/questions/33628779/shared-preferences-between-unity-android-and-ios
        // Once you have the auth token in Unity, you can call APIBuddy.GetInstance().SendGetRequest() in the same way as shown above.  You do not need to call
        // _oauthCallbackServer.StartListening().
    }