Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Auhenticate Instagram and get access token and pass to Unity

Discussion in 'UGUI & TextMesh Pro' started by rajkumar.gurusamy, Dec 8, 2015.

  1. rajkumar.gurusamy

    rajkumar.gurusamy

    Joined:
    Dec 7, 2015
    Posts:
    3
    I am integrating the Instagram into my unity3d project and want the user to authenticate from unity project like Facebook and i want to get the access token from Instagram to unity project automatically after user authenticated. How to achieve this.

    Process steps:
    1. User click the Instagram icon from our Unity scene.
    2. Browser / App will open and ask user to authenticate the application.
    3. After authentication completed, access token will return to Unity scene.

    I have completed all the other steps except getting the access token of Instagram automatically from browser to Unity. I have searched lot to find solution for this problem. Can anybody please give me solution to do this.

    How to get the access token from the browser URL parameters automatically from Unity project? Please anybody share ideas, ref links and way to solve this problem.
     
    ASHOKNATION likes this.
  2. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,016
  3. ramkesh

    ramkesh

    Joined:
    May 15, 2019
    Posts:
    10
    I am able to LogIn but after authentication I am not able to get access token. I have used Web View in Unity. In android there is a method for dialogbox finished and then we can get access token. But in Unity there is no method . I have created one method but still not getting access token . I searched a lot but did not find anyone who have done instagram Log In in Unity.

    Please take a look of Script:

    public class SampleWebView : MonoBehaviour
    {
    public AuthDiaLog auth;
    private string Url;
    public GUIText status;
    WebViewObject webViewObject;
    public Text textM;

    private AuthenticationListener listener;
    public SampleWebView webView;
    private bool authComplete;
    public Uri uri;


    private string url;

    public SampleWebView(AuthenticationListener listener)
    {
    this.listener = listener;
    }

    public void initializeWebview()
    {
    //This is the me
    // webView.Url = url;
    string acces_token;

    //Check URL for acces token
    if (url.Contains("#access_token=") && !authComplete)
    {
    Uri uri = new Uri(url);
    acces_token = HttpUtility.ParseQueryString(uri.Query).Get("access_token");
    acces_token = acces_token.Substring(acces_token.LastIndexOf("=") + 1);
    textM.text = acces_token;
    Debug.Log("AccessToken:" + acces_token);
    authComplete = true;
    listener.onCodeReceived(acces_token);
    return;
    }
    else if (url.Contains("?error"))
    {
    textM.text = "access_Token fetching error" ;
    Debug.Log("access_Token fetching error");
    return;
    }

    }

    IEnumerator Start()
    {
    //this to open dialog for Enter details for auth
    url =Constants.Base_URL + "oauth/authorize/?client_id=" + Constants.Instagra_ClientId + "&redirect_uri=" + Constants.REDIRECT_URL
    + "&response_type=token"
    + "&display=touch&scope=public_content";
    textM.text =url;
    Url = url; //"https://www.google.com/";
    textM.text = Url;
    Debug.Log("Url:"+Url);
    webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
    webViewObject.Init(
    cb: (msg) =>
    {
    Debug.Log(string.Format("CallFromJS[{0}]", msg));
    status.text = msg;
    status.GetComponent<Animation>().Play();
    },
    err: (msg) =>
    {
    Debug.Log(string.Format("CallOnError[{0}]", msg));
    status.text = msg;
    status.GetComponent<Animation>().Play();
    },
    started: (msg) =>
    {
    Debug.Log(string.Format("CallOnStarted[{0}]", msg));
    },
    ld: (msg) =>
    {
    Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
    #if UNITY_EDITOR_OSX || !UNITY_ANDROID
    // NOTE: depending on the situation, you might prefer
    // the 'iframe' approach.
    // cf. https://github.com/gree/unity-webview/issues/189
    #if true
    webViewObject.EvaluateJS(@"
    if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
    window.Unity = {
    call: function(msg) {
    window.webkit.messageHandlers.unityControl.postMessage(msg);
    }
    }
    } else {
    window.Unity = {
    call: function(msg) {
    window.location = 'unity:' + msg;
    }
    }
    }
    ");
    #else
    webViewObject.EvaluateJS(@"
    if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
    window.Unity = {
    call: function(msg) {
    window.webkit.messageHandlers.unityControl.postMessage(msg);
    }
    }
    } else {
    window.Unity = {
    call: function(msg) {
    var iframe = document.createElement('IFRAME');
    iframe.setAttribute('src', 'unity:' + msg);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
    }
    }
    }
    ");
    #endif
    #endif
    webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
    },
    //ua: "custom user agent string",
    enableWKWebView: true);
    #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
    webViewObject.bitmapRefreshCycle = 1;
    #endif
    webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
    webViewObject.SetVisibility(true);

    #if !UNITY_WEBPLAYER
    if (Url.StartsWith("http")) {
    webViewObject.LoadURL(Url.Replace(" ", "%20"));
    } else {
    var exts = new string[]{
    ".jpg",
    ".js",
    ".html" // should be last
    };
    foreach (var ext in exts) {
    var url = Url.Replace(".html", ext);
    var src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
    var dst = System.IO.Path.Combine(Application.persistentDataPath, url);
    byte[] result = null;
    if (src.Contains("://")) { // for Android
    var www = new WWW(src);
    yield return www;
    result = www.bytes;
    } else {
    result = System.IO.File.ReadAllBytes(src);
    }
    System.IO.File.WriteAllBytes(dst, result);
    if (ext == ".html") {
    webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
    break;
    }
    }
    }
    #else
    if (Url.StartsWith("http")) {
    webViewObject.LoadURL(Url.Replace(" ", "%20"));
    } else {
    webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
    }
    webViewObject.EvaluateJS(
    "parent.$(function() {" +
    " window.Unity = {" +
    " call:function(msg) {" +
    " parent.unityWebView.sendMessage('WebViewObject', msg)" +
    " }" +
    " };" +
    "});");
    #endif
    yield break;
    }

    #if !UNITY_WEBPLAYER
    void OnGUI()
    {
    GUI.enabled = webViewObject.CanGoBack();
    if (GUI.Button(new Rect(10, 10, 80, 80), "<")) {
    webViewObject.GoBack();
    }
    GUI.enabled = true;

    GUI.enabled = webViewObject.CanGoForward();
    if (GUI.Button(new Rect(100, 10, 80, 80), ">")) {
    webViewObject.GoForward();
    }
    GUI.enabled = true;

    GUI.TextField(new Rect(200, 10, 300, 80), "" + webViewObject.Progress());

    if (GUI.Button(new Rect(600, 10, 80, 80), "*")) {
    var g = GameObject.Find("WebViewObject");
    if (g != null) {
    Destroy(g);
    } else {
    StartCoroutine(Start());
    }
    }
    GUI.enabled = true;
    }
    #endif
    }


    Thanks in Advance.
     
  4. ramkesh

    ramkesh

    Joined:
    May 15, 2019
    Posts:
    10
    DungDajHjep likes this.