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.Init(
cb: (msg) =>
{
Debug.Log(string.Format(“CallFromJS[{0}]”, msg));
status.text = msg;
status.GetComponent().Play();
},
err: (msg) =>
{
Debug.Log(string.Format(“CallOnError[{0}]”, msg));
status.text = msg;
status.GetComponent().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. Unity.call overrides previous Unity.call · Issue #189 · gree/unity-webview · GitHub
#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.