Advertisement
Guest User

Untitled

a guest
May 12th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. //using System.Net.httpclient;
  9.  
  10. public class OGS : MonoBehaviour {
  11.  
  12. string generateAPIClient = "http://beta.online-go.com/developer";
  13. string APIKey = "0c63a59dd17ec69a48af5d9dc8b4e956";
  14. string requestUserToken = "oauth2/access_token";
  15. string clientID = "";
  16. string clientSecret = "";
  17. string baseURL = "http://online-go.com/";
  18. string url = "";
  19. string username;
  20. string password;
  21. string POST;
  22.  
  23. List<Settings> settings;
  24. // Use this for initialization
  25. void Start () {
  26. Debug.Log("Opened");
  27. settings = new List<Settings>();
  28. Load("Settings");
  29. clientID = AssignSetting("clientID");
  30. clientSecret = AssignSetting("clientSecret");
  31. username = AssignSetting("username");
  32. password = AssignSetting("password");
  33. POST = string.Format( "client_id={0}&client_secret={1}&grant_type=password&username={2}&password={3}",
  34. clientID, clientSecret, username, password);
  35. url = baseURL + requestUserToken;
  36. StartCoroutine("LoadWWW");
  37.  
  38. }
  39.  
  40. //Assign settings loaded to settings variables
  41. string AssignSetting (string item) {
  42. int position = -1;
  43. for(int i=0;i<settings.Count;i++) {
  44. if(settings[i].name == item){return settings[i].value;}
  45. }
  46.  
  47. return string.Empty;
  48. }
  49.  
  50. IEnumerator LoadWWW() {
  51. byte[] byteArray = GetBytes(POST);
  52. Dictionary<string,string> headers = new Dictionary<string,string>();
  53. headers.Add("Content-Type", "application/x-www-form-urlencoded");
  54. WWW text = new WWW(url, byteArray, headers);
  55. yield return text;
  56. byteArray = text.bytes;
  57. string POSTResponse = GetString(byteArray);
  58. Debug.Log(POSTResponse);
  59. Debug.Log(text.responseHeaders);
  60. Debug.Log(text.error);
  61. }
  62.  
  63. static byte[] GetBytes(string str)
  64. {
  65. byte[] bytes = new byte[str.Length * sizeof(char)];
  66. System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
  67. return bytes;
  68. }
  69.  
  70. static string GetString(byte[] bytes)
  71. {
  72. char[] chars = new char[bytes.Length / sizeof(char)];
  73. System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
  74. return new string(chars);
  75. }
  76.  
  77. private bool Load(string fileName)
  78. {
  79. // Handle any problems that might arise when reading the text
  80. try
  81. {
  82. string line;
  83. // Create a new StreamReader, tell it which file to read and what encoding the file
  84. // was saved as
  85. StreamReader theReader = new StreamReader(Application.dataPath + "/Resources/" + fileName + ".txt");
  86. // Immediately clean up the reader after this block of code is done.
  87. // You generally use the "using" statement for potentially memory-intensive objects
  88. // instead of relying on garbage collection.
  89. // (Do not confuse this with the using directive for namespace at the
  90. // beginning of a class!)
  91. using (theReader)
  92. {
  93. // While there's lines left in the text file, do this:
  94. do
  95. {
  96. line = theReader.ReadLine();
  97.  
  98. if (line != null)
  99. {
  100. // Do whatever you need to do with the text line, it's a string now
  101. // In this example, I split it into arguments based on comma
  102. // deliniators, then send that array to DoStuff()
  103. string[] entries = line.Split(':');
  104. if (entries.Length > 0){
  105. Settings newSetting = new Settings(entries[0], entries[1]);
  106. settings.Add(newSetting);
  107. }
  108. }
  109. }
  110. while (line != null);
  111. // Done reading, close the reader and return true to broadcast success
  112. theReader.Close();
  113. return true;
  114. }
  115. }
  116. // If anything broke in the try block, we throw an exception with information
  117. // on what didn't work
  118. catch (Exception e)
  119. {
  120. Console.WriteLine("{0}n", e.Message);
  121. return false;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement