I just recently upgraded from Unity 1.5 to 1.7 and we are finding that some code which we were using to send to twitter is no longer working with WWW.EncodeURL passwords.
I.e. if a user has a @ symbol in their password, it is being encoded fine to %40 before firing off to twitter but something has changed within the Unity implementation that is causing this to come back from the twitter function saying could not authenticate user. If we test with a vanilla user / password with no encoded characters, it works fine.
It all used to work in all cases for Unity 1.5 and I understand they have changed the way unity uses native WWW calls now. Is it possible that a default value (GET, POST etc) is now flipped and causing this error to occur?
Many thanks in advance.
We are using the following code:
string encodedUsername = WWW.EscapeURL(lastEnteredTwitterUsername);
string encodedPassword = WWW.EscapeURL(lastEnteredTwitterPassword);
string twitterUrl = “http://” + encodedUsername + “:” + encodedPassword + “@twitter.com/statuses/update.json”;
WWWForm twitterForm = new WWWForm();
twitterForm.AddField(“dummyField”,“”);
WWW www = new WWW(twitterUrl, twitterForm);
yield return www;
if (www.error == null)
{
string retValue = “” + www.data;
since unity iphone 1.6 the www stuff goes through apples NSURLConnection
As such you would have to comply to that restrictions when sending requests.
also I don’t see why you encode the pwd and username. I would recommend to build the whole url and then encode it. I’m though not sure if the encode is right in that case ( as per Basic access authentication - Wikipedia it expects base64 encode, not 100% sure thats the results you get from encodeurl)
if all goes wrong though you can always go the non basic auth way.
Thanks Dreamora, I don’t quite get what you mean by ‘encode the entire url’. The twitter link I was firing to has an @ symbol in it which I do not want encoded as it is part of the JSON / Login component. Before the 1.6 / 1.7 upgrade the JSON call would not work with complex passwords until you encoded it using URL encoding, and worked as soon as this was added so it seemed to make sense. Also I note that the content type of the WWWForm is set as application/x-www-form-urlencoded
However, we did resolve the issue by custom setting the WWWForm header for Basic Authorization and that seemed to do the trick. I do suggest that anyone who has been using twitter code from these forums in v 1.6 onwards double checks that their code still works for complex passwords (aka ones with URL encodable characters in them like @ symbols etc). You may not even know the unity upgrade has broken this functionality unless you or one of your users has something like an @ symbol in their password.
FYI - Here is the code we used to resolve the issue. Not sure if it was necessary to leave the original username / password in the URL that was broken by the unity update however we were in a mad rush to get these changes out.
string encodedUsername = WWW.EscapeURL(lastEnteredTwitterUsername);
string encodedPassword = WWW.EscapeURL(lastEnteredTwitterPassword);
string twitterAuthorization = encodedUsername + “:” + encodedPassword;
WWWForm twitterForm = new WWWForm();
twitterForm.AddField(“status”,“”);
Hashtable headerData = twitterForm.headers;
byte[ ] dataData = twitterForm.data;
headerData[“Content-Type”] = “application/x-www-form-urlencoded”;
string twitterUrl =“http://” + twitterAuthorization + “@twitter.com/statuses/update.json”;
string user = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(lastEnteredTwitterUsername + “:” + lastEnteredTwitterPassword));
headerData.Add(“Authorization”, "Basic " + user);
WWW www = new WWW(twitterUrl, dataData, headerData);
yield return www;
glad you got it worked.
Did the base64 encoding do the trick in the end or the combination of the whole conversion process?