3
Contributors
26
Replies
93
Views
3 Years
Discussion Span
Last Post by pritaeas
Featured Replies
0

Me too, although I haven't had time to debug yet. It stops after the redirect which returns the code (all I can see so far).

0

I HATE going back to old code. Even though I sort-of-documented it, it's still a chore.

0

Heh heh. Well whenever there's a change to an API, I steel myself for the fallout.

0

Nevermind. It's all good. Apparently the reason it didn't work in the first place was all because of a typo.

0

Glad to hear :) Sorry about that. There was a typo that was precluding my legacy fix from working.

0

This is the response I get from line 28 in the code below:

HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Date: Sun, 08 Dec 2013 10:32:48 GMT
Server: Apache/2.2
X-Powered-By: PHP/5.3.10
Set-Cookie: dani_session=BzFUP1FiDzdZelV5B2gDZAQ0VD1dJFYhBGEEdQIiCGABMlU1CQIHOlFiUiEFOAFwWDpUOARjUT0LKV1sDGcBNARhBmYObFtjVzoGMFFgBmMHZlRkUWAPP1kwVTsHYANmBDxUYl1nVmEEMgQ0AmAIOgFlVTgJZQdlUTVSIQU4AXBYOlQ6BGFRPQspXWcMJwFaBGUGYA45WyhXbgZ7UXYGcAdrVHZRbQ88WTBVMAdwA20EMFQpXTRWagQqBDMCZAgnAW9VaAl%2FB2hRdVI5BTIBM1g6VCkEJFF0C25dfAwIAWQEYwZhDjNbLlcpBjNRZwZoB2BUPlEkDzdZMFU5B2gDdwRoVGZddVYmBFsEZwIyCH0BNFUtCTQHJ1F%2FUiEFOAFqWDpUOgRiUT8LPV06DG4BPQQzBjIOZVthV3gGMlE8BmgHclRwUSQPaFlzVVUHNgM0BHBUZl0kVmkEdwQ8AmEIMwF%2FVXkJZgcu; expires=Tue, 08-Dec-2015 10:32:48 GMT; path=/; domain=www.daniweb.com
Set-Cookie: geolocation=NL; path=/; domain=www.daniweb.com
Set-Cookie: dani_session=VmAFblBjADhXdFB8Uj0GYQY2Bm8IcQN0UDUCcwIiXTVSYQZmBg0EOQMwUiFVaFIjWTtXO1E2VzsEJgBgDTVXZVUwCztQMAxrVWwFOFFgVTJWOQU3UDQAO1c6UDpSNwZkBjEGZghmAzRQZQI4AjJdPlI8Bj4GMwRpAzFSIVVoUiNZO1c5UTRXOwQmADoNJlcMVTQLbVBnDH9VbAV4UXZVI1Y6BSdQbAAzVz5QNVIlBmgGMgZ7CGEDP1B%2BAjUCZF1yUjwGOwZwBGsDJ1I5VWJSYFk7VypRcVdyBGEAIQ0JVzJVMgtsUG0MeVUrBTBRZ1U7VjEFb1AlADhXPlA8Uj0GcgZqBjQIIANzUA8CYQIyXShSZwZ%2BBjsEJAMtUiFVaFI5WTtXOVE3VzkEMgBnDW9Xa1ViCz9QOww2VXoFMVE8VTtWIwUhUCUAZ1d9UFBSYwYxBnIGNAhxAzxQIwI6AmFdZlIsBioGaQQt; expires=Tue, 08-Dec-2015 10:32:48 GMT; path=/; domain=www.daniweb.com
Vary: Accept-Encoding
Content-Length: 1960
Connection: close
Content-Type: text/html; charset=UTF-8

Using this code for a long while:

<?php
session_start();

$clientId = -1;
$clientSecret = '';
$currentUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

if (!isset($_GET['code']))
{
    header("Location: http://www.daniweb.com/api/oauth?client_id=$clientId&redirect_uri=" . urlencode($currentUrl));
    exit();
}

$ch = curl_init('http://www.daniweb.com/api/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'code' => $_GET['code'],
    'redirect_uri' => $currentUrl,
    'client_id' => $clientId,
    'client_secret' => $clientSecret
));

$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode == 301 or $httpCode == 302) {
    preg_match("@https?://([-\w\.]+)+(:\d+)?(/([\w/_\-\.]*(\?\S+)?)?)?@", $result, $m);
    $targetUrl = $m[0];
    $urlParts = parse_url($targetUrl);
    parse_str($urlParts['query'], $queryParts);
    $token = $queryParts['access_token'];
    $_SESSION['DwApiAccessToken'] =  $token;
    header('Location: http://dwapi.pritaeas.net/');
}
?>

Edited by pritaeas

0

That code should work. Been investigating for like 10 minutes already. Give me another 5 or 10 and then I need to hurry out to a christmas party.

0

Try changing curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); to curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); ... Did you change that at any time?

0

Incidentally, you were getting the 400 error b/c there was actually a 500 error in my code ... Somewhere internally, I ran json_decode() without setting the second parameter to true, and I was trying to fetch its property as if it were an array instead of an object.

0

I set follow to true, but still no go. I get the HTML page for the login page back from curl now. Perhaps my regex is off now, because the header contains the access_token.

See the file. It has Location (redirect) back to my site with the access_token. Probably some change that fails the regex.

Edited by pritaeas

Attachments
HTTP/1.1 100 Continue HTTP/1.1 302 Found Date: Sun, 08 Dec 2013 18:22:59 GMT Server: Apache/2.2 X-Powered-By: PHP/5.3.10 Set-Cookie: dani_session=BjBXPFFiATkFJgAsAm0GYQo6VTwIcQF2VTAKewUlAGgCMQ5uCQJVaAMwUiFUaQZ3UjBTPw9oVTlRcwIzWmdTbgMwBDEEMg0%2BAjtTOwlqBmUGNFc3UWIBNQU%2BAG8CbgZgCjpVPgg2ATJVYwo6BTIAZQJqDmUJOFUwAzBSIVRpBndSMFM9D2pVOVFzAjhacVMIA2IEYgQzDX4CO1MuCS4GcAZqV3VRbQEyBWkAZQJ1BmEKPVU1CH0BN1VgCjwFeAAzAmYOLglsVTIDZVIhVGkGd1IwUz0PalU5UXMCJFpyUzIDcQRZBDYNawI7UzMJKQZwBmpXZFFtATMFZgAsAm0GYQo5VTwIcQFpVTQKewUiAF4CPw5jCSlVaAMiUmpUJgZ9UihTNw8zVTlRYAJiWjlTYQM2BDQEYQ01AmlTZAlmBiEGa1c%2FUW0BIQUoACwCMgYiClVVYggyAXFVNAoqBW0AcgJkDjAJZ1UjA3ZSOFQv; expires=Tue, 08-Dec-2015 18:22:59 GMT; path=/; domain=www.daniweb.com Set-Cookie: geolocation=US; path=/; domain=www.daniweb.com Set-Cookie: dani_session=VmBROlRnUWlZegcrA2wGYQs7Bm9aI1otVzIBcFNzAGhcb1AwU1gCP1JhBnVdYAZ3WDpfMwRjB2sGJAUwDmxbago5U2MCNQozBjlSPVU4AWRWN1ExVDRRZFljBzwDMAYyCzoGNlowWjxXZQFiUzcAY1xlUGxTMwI3UmMGdV1gBndYOl8xBGEHawYkBT8OJVsACmtTNQI1CnkGP1IvVXIBd1Y6UXNUaFFiWTUHYgN0BmELPAZmWi9abFdiATdTLgAzXDhQcFM2AmVSNAZ1XWAGd1g6XzEEYQdrBiQFIw4mWzoKeFMOAjAKbAY%2FUjJVdQF3VjpRYlRoUWNZOgcrA2wGYQs4Bm9aI1oyVzYBcFN0AF5cYVA9U3MCP1JzBj5dLwZ9WCJfOwQ4B2sGNwVlDm1baQo%2FU2MCZwoyBm1SZVU6ASZWO1E5VGhRcVl0BysDMwYiC1QGMVpgWipXNgEhUzsAclw6UG5TPQJ0UicGbF0m; expires=Tue, 08-Dec-2015 18:22:59 GMT; path=/; domain=www.daniweb.com Location: http://dwapi.pritaeas.net/DwApiAuthorize.php?access_token=BSlUJ1AjVDhYaQQqBycBMgAiUjsJcAQwAWBQNAVjADJTag8yBWEHYlczAmRSZA5qUztUaVZlVmMEYFU1XGZUNVYyBmAPPgppXDIBPwdlA2IFYlQ0UGFUdFgsBHkHJwEkAGVScwk7BGQBI1BoBXQAOFM7DzYFYAdqVyICf1J2Dn5TblRvVmpWcgRhVWVcc1QPViMGdg81CilcOgEtBzsDIwUmVHVQbFQKWC8EBwd9ATMAd1JgCSIEaQEvUCIFJABoU3sPYAU0BzJXcwJ9UjoOaVN/VFdWLFZEBHNVR1x3VDlWFwZxDygKY1xvAX0HOgMtBTdUK1AmVD5YcAR5B34BdQBjUm0JOwRlAW9QJgUJAGhTaw8jBWsHcVcxAmBSdg4gUylUblZ7VnAEbVV0XGJUI1Z0Bj4PbQo4XDgBOQdlA2YFYVQ2UGFUYVh9&expires=1386613377 Vary: Accept-Encoding Content-Length: 0 Content-Type: text/html; charset=UTF-8 HTTP/1.1 302 Moved Temporarily Date: Sun, 08 Dec 2013 18:23:00 GMT Server: Apache Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: PHPSESSID=r01h4comph18qa773ba01vu756; path=/ Location: http://www.daniweb.com/api/oauth?client_id=13&redirect_uri=http%3A%2F%2Fdwapi.pritaeas.net%2FDwApiAuthorize.php Vary: Accept-Encoding Content-Length: 0 Content-Type: text/html HTTP/1.1 302 Found Date: Sun, 08 Dec 2013 18:23:00 GMT Server: Apache/2.2 X-Powered-By: PHP/5.3.10 Set-Cookie: csrf_cookie=a345ee48f6f510ed90d374f34c037d93; expires=Sun, 08-Dec-2013 20:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: dani_session=V2EAa11uDzcAIwMvAW4AZwExAGlbIgN0AmcDcgEhAWlSYQ5uBg0GOwQ3VSZWawNyA2FQPAZhBGgCIAJnCWVTMwJgVjdSOQoyVD4PNgViBDZXYgA0XTgPPQA7A2UBZABiATcAYltkAzICMAM1ATABZVJtDjgGYAY3BDFVJlZrA3IDYVA%2BBmMEaAIgAjgJIlMIAmNWMFJlCnlUbQ9yBSIEclc7ACJdYQ88AGwDZgF2AGcBNgBgWy4DNQI3AzUBfAEyUjYOLgZjBmEEYlUmVmsDcgNhUD4GYwRoAiACJAkhUzICcFYLUmAKbFRtD28FJQRyVzsAM11hDz0AYwMvAW4AZwEyAGlbIgNrAmMDcgEmAV9Sbw5jBiYGOwQlVW1WJAN4A3lQNAY6BGgCMwJiCWpTYQI3VmZSNwoyVDAPMQVqBCNXOgBoXWEPLwAtAy8BMQAkAV4AN1thA3MCYwMjAWkBc1I0DjAGaAZwBHFVP1Yt; expires=Tue, 08-Dec-2015 18:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: geolocation=US; path=/; domain=www.daniweb.com Set-Cookie: dani_session=BDJROl1uU2tWdVB8Am1SNQs7UzpYIVYhCm8EdQ8vXTUCMQ9vBwxUaQIxAXJSbw9%2BWTtSPgJlC2cDIQFqXTFaaQdmVzZTMF5sVDpSOVZhUTMEMFE0XWJTNFY9UD4CNVJnC21TZFg0VmMKOQQzD25dbwJoDzIHalQzAjYBclJvD35ZO1I8AmcLZwMhATtddloBB2ZXMVNkXi1UbVIvVnFRJwRoUXNdYVNgVjpQNQJ1UjULPFMzWC1WYAo%2FBDIPcl1uAmYPLwdiVDMCZAFyUm8Pflk7UjwCZwtnAyEBJ111WjsHdVcKU2FeOFRtUjJWdlEnBGhRYl1hU2FWNVB8Am1SNQs4UzpYIVY%2BCmsEdQ8oXQMCPw9iBydUaQIjATlSIA90WSNSNgI%2BC2cDMgFhXT5aaAcyV2dTNl5mVDBSbFY5UXYEaVE5XWFTc1Z7UHwCMlJ2C1RTZFhiViYKawQkD2ddLwJkDzEHaVQiAncBa1Ip; expires=Tue, 08-Dec-2015 18:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: dani_session=BTMCaQExATlQc1J%2BDmEFYlVlXDUAeQdwC24Dcld3XTUFNlIyAgkGOwAzWygAPVYnVjRfM1M0Am4GJABrXzMHNAdmUTBSMV1vAG4HbFViA2EFMQJnAT4BZlA7UjwOOQUwVTNcawBsBzILOAM0VzZdbwVvUm8CbwZhADRbKAA9VidWNF8xUzYCbgYkADpfdAdcB2ZRN1JlXS4AOQd6VXIDdQVpAiABPQEyUDxSNw55BWJVYlw8AHUHMQs%2BAzVXKl1uBWFScgJnBmEAZlsoAD1WJ1Y0XzFTNgJuBiQAJl93B2YHdVEMUmBdOwA5B2dVdQN1BWkCMQE9ATNQM1J%2BDmEFYlVmXDUAeQdvC2oDcldwXQMFOFI%2FAiIGOwAhW2MAclYtVixfO1NvAm4GNwBgXzwHNQcyUWF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cDaAU0VmZVIlRxB24PLVBjUGEGPAsqV3cHIw88VD8BIQZ9DzNdKQZ6DmFVcwIzAXYAMwBjAGRQZ1R5UmIFc1ApUGxfcVJ2BWYFZldqCSgFOgNiBT9WbFVnVGYHdQ8tUHxQYAZzCylXPwc9DyZUNAEsBnQPPV0uBi4OeVUhAjcBJgAhAGoAa1BrVGpScwU6UD9QbV93UnYFIwVvV2EJZAU1A2QFIVY5VSBUPgdyDzdQOVA4BmkLf1cxBzgPNVQjAWkGPg8yXTkGLQ46VXMCMwEwADQAdABnUHpUKVI8BSBQalAyXzdSYwU%2FBSVXbAl8BS0DfQVrVi1VLVRyB3YPelAmUGsGMgszVz4HIw8xVDIBLwZnDzNdMQZ1DmFVcQI%2FAXkAPgBnAHdQfFRjUjgFIVA1UHBfdFI5BWsFdFdhCVcFLQN0BSFWZ1U%2FVGYHbg9pUG1QKQYwCzFXPgcxDzpUJAFeBm0POF1hBmsOM1UnAiQBMwA1AG8AcFBtVGhScwUMUCVQcV9tUmsFbQVzV3AJeAVjAyIFflZmVXVUZAdxD2RQJlB%2FBiELNFcjBzUPMVQxAXIGKg8yXTkGLg4vVUUCIQEXACEAbwBDUH1Uf1JvBTxQIlBqX35SMwUrBXdXbAl4BX8DaQU4VnFVclRpB2APdFA1UH8GMgs6VzIHdg9vVC0%3D; expires=Tue, 08-Dec-2015 18:23:00 GMT; path=/; domain=www.daniweb.com Location: http://www.daniweb.com/members/join/page Vary: Accept-Encoding Content-Length: 0 Content-Type: text/html; charset=UTF-8 HTTP/1.1 200 OK Date: Sun, 08 Dec 2013 18:23:00 GMT Server: Apache/2.2 X-Powered-By: PHP/5.3.10 Set-Cookie: csrf_cookie=5fac36b6e2b21c660ff4f427552e5a65; expires=Sun, 08-Dec-2013 20:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: dani_session=Cz0CaVZlUWlVdgAsDmEGYQs7UjtbIlAnBmMEdVNzAWkCMQRkAwgDPgAzBnUCP1UkUDICblUyVjoGJAcwAGlUYgAwUGBUYQpqXWcFbwJuU2ILZAIwVmBRZ1U0AG4OOQZgCztSNVsyUGUGPgQzU2EBNAJvBDwDNAM0AGAGdQI%2FVSRQMgJsVTBWOgYkBz0AK1QPAGFQNlRjCnldZAV4AiVTJQtnAiBWalFiVTkAZQ55BmELPFIyWy5QZgYzBDJTLgEyAmYEJANmA2QAZgZ1Aj9VJFAyAmxVMFY6BiQHIQAoVDUAclANVGYKbF1kBWUCIlMlC2cCMVZqUWNVNgAsDmEGYQs4UjtbIlA4BmcEdVN0AV8CPwRpAyMDPgAhBj4CcFUuUCoCZlVpVjoGNwdnAGNUZgA1UGBUMQoyXTkFOwJtU3QLZgJqVmpRcVV4ACwOPgYiC1RSZVthUCAGZwQkUzsBcwJkBDoDbQN1AHUGbAJ5; expires=Tue, 08-Dec-2015 18:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: geolocation=US; path=/; domain=www.daniweb.com Set-Cookie: dani_session=ADYFbgEyVm5RcgQoAW5QNwMzUDkMdQZxAWRWJwQkC2MGNQVlBQ4HOgIxAnEFOAZ3BGZWOlUyAm4KKABlDG8GNgAyAmECMF5uVWwEPAdjVmEAMwVnATFWN1E%2BBD4BZ1BiAzZQZwxhBmABYlY2BGALPQZsBW8FYQdhAjACcQU4BncEZlY4VTACbgooADoMJwZdAGECZAI1Xi1VbAR5ByBWIABsBScBPVZlUT0EYQF2UDcDNFAwDHkGMAE0VmAEeQs4BmIFJQVgB2ACZAJxBTgGdwRmVjhVMAJuCigAJgwkBmcAcgJfAjBeOFVsBGQHJ1YgAGwFNgE9VmRRMgQoAW5QNwMwUDkMdQZuAWBWJwQjC1UGOwVoBSUHOgIjAjoFdwZ9BH5WMlVpAm4KOwBgDG8GNAA1AjICZ15mVTEEOgdoVnEAbQVtAT1WdlF8BCgBMVB0A1xQZww2BnYBYFZ2BGwLeQZgBTsFawdxAncCaAV%2B; expires=Tue, 08-Dec-2015 18:23:00 GMT; path=/; domain=www.daniweb.com Set-Cookie: referer=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=www.daniweb.com Vary: Accept-Encoding Content-Length: 7059 Content-Type: text/html; charset=UTF-8
0

Sorry, I'm confused by what you're showing me. You shouldn't have had to change anything. The bug was on my end and I was successfully able to log into your site earlier today.

0

I was successfully able to log into your site earlier today.

Really? I tried just before posting (and now), and still don't get back to my main page. Now I'm confused. How can you login, yet I can't.

K, NOW it works for me. I've changed line 32 above to include HTTP code 200. Apparently I'm getting a 200 even though there's a status 302 in the returned header.

I have the feeling the flow (on my end) is still not entirely correct.

Edited by pritaeas

0

Prit, when you changed the FOLLOWLOCATION option from false to true, you're now no longer returning the original status headers (which would be 302) but you're following them to where they take you and returning the final status headers after the redirect (which would be 200).

0

What did you change??

Nothing (only the 200 status check). No clue as to why this is happening, and no time to delve deeper. This morning it worked, now it doesn't.

Edited by pritaeas

0

It's set to false. I've also removed the 200 check, so the code is exactly as above. Just tried and it worked on the second click of my authorize link.

Closed and re-opened IE, and still worked on the second click. Strange it no longer works on the first. As soon as I get some more time I'll try to figure it out.

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.