1

The question is clear I hope. I want log in to DeviantArt through my application and I found stuff but it just doesn't seem to work... Here's my code(running in a thread):

//===========================================
//                 Login
//===========================================  
string formUrl = "https://www.deviantart.com/users/login";
    string formParams = string.Format("login-username={0}&login-password={1}", "mai username", "mai password");
    string cookieHeader;
    System.Net.WebRequest req = System.Net.WebRequest.Create(formUrl);
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
    byte[] bytes = Encoding.ASCII.GetBytes(formParams);
    req.ContentLength = bytes.Length;
    using (Stream os = req.GetRequestStream())
    {
        os.Write(bytes, 0, bytes.Length);
    }
    //=========Send Cookie=============
    System.Net.WebResponse resp = req.GetResponse();
    cookieHeader = resp.Headers["Set-cookie"];
    string pageSource;
    using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
    {
            pageSource = sr.ReadToEnd();
    }
    string getUrl = "http://www.deviantart.com/users/loggedin";
    System.Net.WebRequest getRequest = System.Net.WebRequest.Create(getUrl);
    getRequest.Headers.Add("Cookie", cookieHeader);
    System.Net.WebResponse getResponse = getRequest.GetResponse();

I get a 403 error at the last line where I'm trying to get response. Some information about DeviantArt: 1: You can access pages etc. but you can't comment or anything. 2: When I tried to check if deviantart.com existed it gave me a 403 no permission error so that's why I'm trying to log in. When I write pageSource to a textfile I get this. What do I do wrong and how do I fix it? because I really have no clue why this isn't working...

PS: Alternate ways to do this are also welcome except Selenium

Community
  • 1
  • 1
DFSFOT
  • 536
  • 3
  • 19
  • I am curious about this line `string formParams = string.Format("login-username={0}&login-password={1}", "mai username", "mai password");` have you tried changing the string params to pass in actual username / password values in your string.Format function.. have you stepped through the code to identify what line the error is being thrown..? – MethodMan May 08 '15 at 14:54
  • @MethodMan ofcourse I've changed them to my actual username and password, just changed it to post here... This is throwing an error: `System.Net.WebResponse getResponse = getRequest.GetResponse();` and it's giving a 403 error saying I have no permission – DFSFOT May 08 '15 at 14:59
  • Could you analyse the actual response with a program like fiddler and post the Raw response data (including headers etc.)? This would probably clear some things up. Also to make sure you're sending a proper login request you can use fiddler to compare the request your code sends with the request that is made when you login through the browser. – Nils O May 08 '15 at 15:16
  • If you want to get data from DeviantArt, they have an API (https://www.deviantart.com/developers/authentication), which is probably a better route than this. Furthermore, I think you need to UrlEncode the email and password if you want your current method to work (e.g. `string.Format("login-username={0}&login-password={1}", HttpUtility.UrlEncode("username"), HttpUtility.UrlEncode("password"));`) - this might explain why you're receiving a 403 – adaam May 08 '15 at 15:16
  • Analysing the response body you posted shows this relevant line: `The password you entered was incorrect.`. Skipping out on UrlEncoding as adaam suggests could definitely be the problem – Nils O May 08 '15 at 15:18
  • @adaam I found smth else, if that doesn't work I'll try that. – DFSFOT May 08 '15 at 15:30
  • @NilsO I'm 100% sure the credentials I enter are right, I think the skipping causes the problem yea – DFSFOT May 08 '15 at 15:30
  • 1
    FYI, all you need is to get another 27 points here on Stack Overflow (for a total of 200)  and you’ll get the [association bonus](https://stackoverflow.com/help/whats-reputation), which will give you 100 points on all Stack Exchange sites (allowing you to comment everywhere). – G-Man Says 'Reinstate Monica' Jan 10 '20 at 00:20

0 Answers0