Seller Central
Seller Forums

Amazon Seller Forums » Login With Amazon » Questions about Login With Amazon

Thread: LWA using JS: Array Warning: urlencode() expects parameter 1 to be string,
This question is not answered.


Permlink Replies: 1 - Pages: 1 - Last Post: 26 Apr, 2017 3:57 PM by: Anurag S. (LWA) Threads: [ Previous | Next ]
vs_app

Posts: 1
Registered: 24 Apr, 17 5:15 PM
Posted on: 24 Apr, 2017 11:22 AM  
Click to report abuse...   Click to reply to this thread Reply
Hello,

I am trying to use Javascript sdk and am running my website on cloud9 server.
My client login file is labelled login.html and code posted below:

<html>
<head>

</head>
<body>
<!--- this first script loads lWA sdk for JS--->

<script type="text/javascript">

window.onAmazonLoginReady = function() {
amazon.Login.setClientId('<client_id>');
};
(function(d) {
var a = d.createElement('script'); a.type = 'text/javascript';
a.async = true; a.id = 'amazon-login-sdk';
a.src = 'https://api-cdn.amazon.com/sdk/login1.js';
d.getElementById('amazon-root').appendChild(a);
})(document);

</script>

<!-- this script is for button-->

Login with Amazon



<!-- this script loads the login page on clicking the button -->


<script type "text/javascript">

document.getElementById('LoginWithAmazon').onclick = function() {
setTimeout(window.doLogin, 1);
return false;
};

window.doLogin = function() {
options = {};
options.scope = 'profile';
options.response_type= 'code';

amazon.Login.authorize(options, "https://lwa-4-vs93.c9users.io/my_login.php");
};

</script>

</body>
</html>


On the server side, I am running my_login.php (code below):

<?php

// Verify that the access token belongs to us
// The token must be url-encoded when passed to tokeninfo

echo $_REQUEST;

$c = curl_init('https://api.amazon.com/auth/o2/tokeninfo?access_token=' . urlencode($_REQUEST));
curl_setopt($c, CURLOPT_RETURNTRANSFER,true);

$r = curl_exec($c);
curl_close($c);
$d =json_decode($r);
/*
echo $d;

echo $r;

echo $d;
*/
if ($d->aud != '<client_id>') {
// the access token does not belong to us
header('HTTP/1.1 404 Not Found');
echo 'Page not found';
exit;
}

// Exchange the access token for user profile
// The token must NOT be url-encoded when passed to profile

$c = curl_init('https://api.amazon.com/user/profile');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Authorization: bearer'. $_REQUEST));
curl_setopt($c, CURLOPT_RETURNTRANSFER,true);

$r = curl_exec($c);
curl_close($c);
$d =json_decode($r);

echo sprintf('%s %s %s', $d->name, $d->email,$d->user_id);

?>


The _echo $_REQUEST;_ command says this error:

Array Warning: urlencode() expects parameter 1 to be string, array given in /home/ubuntu/workspace/my_login.php on line 8 Call Stack: 0.0015 237224 1. {main}() /home/ubuntu/workspace/my_login.php:0 0.0015 237472 2. urlencode() /home/ubuntu/workspace/my_login.php:8 Page not found

I have been following the tutorials as stated here :

It would be great, if you could help debug this issue.

Thanks

Edited by: Anurag S. (LWA) on Apr 26, 2017 3:47 PM
Is this a helpful question? Yes 0   No
1 Reply
Sort replies by:
Anurag S. (LWA)

Posts: 19
Registered: 16 Oct, 14 12:50 AM
Posted on: 26 Apr, 2017 3:57 PM   in response to: vs_app in response to: vs_app  
Click to report abuse...   Click to reply to this thread Reply
Hi,
Thanks for reaching out.

Looking at your code snippets it seems like there is a minor syntax error on line 8.
I suggest you try changing the line 8 from something like:

$c = curl_init('https://api.amazon.com/auth/o2/tokeninfo?access_token=' . urlencode($_REQUEST));

to

$c = curl_init('https://api.amazon.com/auth/o2/tokeninfo?access_token=' . urlencode($_REQUEST));

Thanks,
Anurag
Login With Amazon
Was this reply helpful? Yes 0   No 0