Retrieving data from polar accesslink via php sdk - php

I am attempting to use the polar php sdk to retrieve data from the polar accesslink api. I am able to get through the Oauth2 workflow but am stuck after receiving the access token. Following the documentation I am attempting to register a user using the following code:
$config = Configuration::getDefaultConfiguration()->setAccessToken($this->token);
$apiInstance = new UsersApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new \GuzzleHttp\Client(),
$config
);
$body = new Register(); // \Coachbox\Services\Polar\Models\Register |
try {
$result = $apiInstance->registerUser($body);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling UsersApi->registerUser: ', $e->getMessage(), PHP_EOL;
}
However the sdk throws the following error: [400] Client error: POST https://www.polaraccesslink.com/v3/users resulted in a 400 Bad Request
If instead use a curl command as follows (but through php):
curl -X POST https://www.polaraccesslink.com/v3/users \
-H 'Content-Type: application/xml' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer {access-token}'
Body parameter
{
"member-id": "User_id_999"
}
I get an empty response. I feel I must be missing an important step, but I do not know what it is. Any help is greatly appreciated.
EDIT:
Heard back from Polar Support, there is an error in their API documentation. The -H 'Content-type' should be 'application/json'. But, if I make the change I get a new error:
"Unrecognized token 'member': was expecting ('true', 'false' or 'null')
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 8]"
EDIT 2: I was able to get the code working through a cURL request (I didn't have my body formatted as json properly). However, I am still unable to get the sdk working.

Did a bit of debugging on the same issue and it turns out the SDK example misses the vital step of setting the member_id
It should be:
$body = new Register();
$body->setMemberId($your_member_id);

Related

php discogs API auth flow

I'm trying to use the discogs php api in a PHP script to get info on a release using the release ID. For example, when I make a request to:
http://mywebsite.com/test.php?id=1017868
I want to call the discogs API to get info on the release with id = 1017868. I can see the info I want by manually going to:
https://api.discogs.com/releases/1017868
So I have my $consumerKey =and $consumerSecret and I'm following the DISCOGS AUTH FLOW instructions, which says I can send my keys in a get request like so:
curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
I'm trying to make a get request for my target id in my php script like so:
<?php
echo "hello world <br>";
//discogs simple auth flow, http requests
$remote_url = 'https://api.discogs.com/releases/1017868';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Discogs key=mykeyasdlkhaskld, secret=mysecretkeykjnasdkjnsadkj"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
echo "end of program";
?>
But I keep getting the error:
"failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden".
Is there something I'm forgetting for making my request to the server? Thanks.

How to send push to specific devicetoken on php function

I'm Using Ionic Framework V1.
I've already get push notifications through Postman using
and I've already save all devicetokens in my DB, but i'm stuck at getting any push for specific devices in php functions.
curl -X POST -H "Authorization: Bearer API_TOKEN" -H "Content-Type: application/json" -d '{
"tokens": ["DEV_DEVICE_TOKEN"],
"profile": "PROFILE_NAME",
"notification": {
"message": "This is my demo push!"
}
Example:
public function confirmAction($request){
$action = $this->find($request->idtblAction);
$action->idtblStatus=1;
if($action->save()){
//I need to send notification in this part
return Response::json(array('success'=>'true'));
}else{
return Response::json(array('success'=>'false'));
}
}
Ionic io uses Bearer key and profileapp
Any idea?
I've already solve this without using php. I did it through my internal ionic app code.
Thank you to everybody!

Basic Authorization using CURL and Fastbill API

i'm having a hard time authorizing a user login since i just dont know the right syntax. I want to modify https://github.com/ZWEISCHNEIDER/fastbill to enable said login API-wise.
The current header bit looks like this and works
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'header' => 'Authorization: Basic ' . base64_encode($this->email.':'.$this->apiKey)
)
);
With this i can connect to the general API and get all the information. In the documentation of the API it requires me to put the following additional info into the header (left -u line there for better understanding) in order to receive user-bound information only.
-u {E-Mail-Address}:{API-Key} \
-H 'X-Username: {User-Emailaddress}'\
-H 'X-Password: {User-Password}' \
How do i adapt the 'header' content-string to fit X-Username:$username and X-Password:$pwd in there?
(api doc: https://www.fastbill.com/api/fastbill/en/fundamentals.html#authentification)
thanks in advance
ok i'm sorry, this did not have anything to do with malformed header requests from my side -- the api simply won't let me access like i planned using a regular api key

POST json returns 400 with curl but functional test working

I am trying to build the POST of an API using Symfony2 and FOSRestBundle. The following functional test returns OK.
public function testPostArticleAction(){
$this->client->request(
'POST',
'/api/v1/articles.json',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{"articleContent":"The content of the content"}'
);
$response = $this->client->getResponse();
$this->assertJsonResponse($response,201, false);
}
But when I try to send a request via Curl with the same request body, it gives me a 400 invalid json message:
{"code":400,"message":"Invalid json message received"}
Here are the curl commands I have tried:
curl -X POST -d '{"articleContent":"title1"}'
http://localhost:8000/api/v1/articles --header
"Content-type:application/json"
curl -X POST -d '{"articleContent":"title1"}'
http://localhost:8000/api/v1/articles.json
Please to note that the GET returns to me a json like:
{"id":68,"article_content":"contents contents"}
But my field is articleContent in my doctrine mapping file. What am I missing?
Your help is much appreciated.
Try updating your header option to:
"Content-Type: application/json"
Additionally - does this request require any type of authentication? You'd need to pass in an auth cookie if so.

HTTP_Request2 POST parameters ignored?

A query with curl, like this, works fine:
curl -XPOST -H "Content-Type: application/json" -d '{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}' http://localhost:8080/services/rest/index/z56508/search/field/search
I my case I get 11 hits there. However when I try to translate this to HTTP_Request2 the call returns all hits in database instead.
I have looked at Http_Request2 POST request not working to write the code here:
require_once 'HTTP/Request2.php';
$url = "http://localhost:8080/services/rest/index/z56508/search/field/search";
$data = array("query"=>"porges","start"=>"0","rows"=>"10","lang"=>"ENGLISH");
$r = new HTTP_Request2($url);
$r->setHeader('Content-Type', 'application/json;charset=UTF-8');
$r->setMethod(HTTP_Request2::METHOD_POST)
->addPostParameter($data);
$response = $r->send();
$body = $response->getBody();
echo $body;
What am I doing wrong? It seems like "query" => "porges" is ignored, but why?
Read the Friendly Manual: http://pear.php.net/manual/en/package.http.http-request2.request.php#package.http.http-request2.request.body
addPostParameter() should be used when you want to generate a POST request body according to application/x-www-form-urlencoded or multipart/form-data Content-Type rules (hint: this is not JSON you are trying to send). If you have a pre-built request body, use setBody():
$request->setBody('{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}');

Categories