简体   繁体   中英

“No 'Access-Control-Allow-Origin' header is present on the requested resource” error when sending request to Jawbone UP API

I've built a custom database that I would like to populate with some of my UP3 data. I've successfully authenticated and received the JSON response with my Bearer token. From here, I'm sort of lost.

From the documentations, I need to send a GET request to

https://jawbone.com/nudge/api/v.1.1/users/@me

Each time I do, I get:

No 'Access-Control-Allow-Origin' header is present on the requested resource

I've added:

header('Access-Control-Allow-Origin: http://example.com'); 

(replacing example.com with my domain)

to the page that is sending the GET request. Any thoughts?

The Jawbone endpoints all have the CORS header set to

Access-Control-Allow-Origin: *

However, this still will not allow cross-site requests from localhost (thanks @TirthrajBarot for the explanation in the comments), so you should disable this check in Chrome with something like the Access-Control-Allow-Origin extension .

After authenticating the user and permissions, I redirect to another page to validate the authentication code and get the access token. From there, I go to another page to send the GET request for the data. Here is the code that is working:

    $url = "https://jawbone.com/nudge/api/v.1.1/users/@me/sleeps?" . $sleepDate;

    $options = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept: application/json\r\n" .
                  "Authorization: Bearer " . $accessToken . "\r\n" . 
                  "Host: {my website}"
      )
    );

    $context = stream_context_create($options);
    $sleepData = @file_get_contents($url, false, $context);

The data is returned, decoded, and processed. The result is pushed into a text box in a form for the user to edit if desired. There is also a button to enter the data into the database (along with a bunch of hidden form fields that enter other relevant details)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM