How to get data from fitbit using PHP - php

I'm trying to get user data from fitbit using fitbit api but the process seems confusing to me. I have tried several ways but could not succeed. I have searched in the internet for solution and found this
http://www.staze.org/retrieving-steps-data-fitbit-api/
But this process is also not working for me.
How to fix the code I mentioned to get data from fitbit or is there any other way to get data using PHP?

You need to do lot of homwwork Dude ;) Anyways...
To access Fitbit Data you can use OAuth 2.0 authentication
You need to pass tokenType and accessCode in any ajax call and you will get response
Now to get accessCode you need to register an App on Fitbit site
You can read API doc for more details
Here is sample code how to access Fitbit Profile using tokenType and accessCode
and I can assure you this is working Code so if your code doesnt work
it means you are doing somthing wrong in above steps
function getProfile() {
var authCode = token_type + " " + access_token;
$.ajax({
url: "https://api.fitbit.com/1/user/-/profile.json",
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", fitbitAccessCode);
},
success: function(jsonObject) {
// logic ...
},
error: function(error) {
alert("Error");
}
});
}

Related

Asana project is created but ajax readystate 0

I have a php application that accesses Asana API. I am able to create a project in Asana. However, the ajax call to the API class is returning a readystate=0.
While troubleshooting in firebug I also noticed that the network console has a 302, 400(??), and 200 status code. I thought 400 status code is related to invalid request or malformed url, but the project gets created anyway.
Any idea?
Update: More information.
I have a Ajax call to a php file which intern calls Asana API to getAuth code and tokens before calling the API services.
I believe I am getting the CORS warning and hence the readystate=0 and the 400 error. However because rest of my script proceeds with the token it was inserting records anyways. However, after the tokens expired (3600 sec), now I am unable to insert records. I know if I call the php file directly it works without the CORS error.
$.ajax({
type: 'POST',
url: "oa/asana.php",
data: {apiprovider:"asana",type:"addnewproject",notes:"notes",name:"name",id:"id",resource:"projects"},
//dataType:"json",
//contentType: "application/json",
success: function(data) {
console.log(data);
},
error: function( error )
{
console.log("asana api error");
console.log(JSON.stringify(error)) ;
},
async:true
});
my php code looks like this.
...$asana = new AsanaAuth(API_KEY,API_SECRET,callbackUrl);
if (!isset($_GET['code'])) {
$url = $asana->getAuthorizeUrl();
header('Location:' . $url);
} else {
$asana->getAccessToken($_GET['code']);
if ($asana->hasError()) {
echo 'Error response code: ' . $asana->responseCode;
}
else {
echo $asana->response;
}
}
Is there a better way to do this outside of Ajax call?
OK here's how I fixed it. Partly my understanding was wrong and I made a wrong assumption.
This is purely based on my application's need and this may not be applicable to all.
I have a settings page where the user clicks on the Asana app to authorize the connection. This is not a Ajax call but a hyperlink to a file which is also my redirect uri. Within this file I check if the user has already authorized if not I call authorize, get tokens and store the refresh token in the database for future use.
In the other parts of my application, when the user clicks on create an asana project, I check for the validity of the access token if it has expired, I refresh and get another token again.
Earlier, I was trying to call this from one page (which in a normal user flow will never happen -in my application). So right now there is no Ajax call for authorize but calling the refresh token, and Asana API endpoints are through Ajax and they work fine.
Hope this helps someone in future.

Empty data response from Facebook API, using app access token

Code that I had written to fetch a Facebook page's posts using an app access token has suddenly stopped working. The JSON response's data object is an empty array. I'm not getting any error messages. When I try making the same request using an access token generated by the Graph API explorer, I get back a valid JSON response containing data. I've tried resetting my app's secret and generating a new access token, but I still get back an empty data object.
When I use the Facebook access token debugger, the only information returned is the App ID.
I generated my app access token by making a GET request to https://graph.facebook.com/oauth/access_token?client_id=<app_id>&client_secret=<app_secret>&grant_type=client_credentials
I don't have control over the page I'm trying to fetch data from, but I can contact the admin. Are there any permissions I should have them check?
Without any error messages or feedback, I'm at a loss on how to debug this. Does anyone have any advice?
This works perfectly.
Demo
token = 'valid_access_token';
page = '20531316728';
url = 'https://graph.facebook.com/'+page+'/feed?access_token="+token+"&fields=id,message&limit=10';
comments = [];
$.ajax({
'async': false,
'url': url,
'dataType': "json",
'success': function (data) {
$('.result').html(JSON.stringify(data));
},
'error': function(data){
$('.result').html(JSON.stringify(data));
}
});

Can I use the Facebook PHP SDK in Spotify apps?

I need to be able to post information from my application to the user's timeline. Now, the only way I know how to do that is with the Facebook PHP SDK. The thing is, since I use the standard JavaScript way to connect with Facebook's login, when I try to get the user id with the PHP SDK, it says it's 0. Is there any way I can use the PHP SDK, or is there just no way to post to Facebook from a Spotify application?
Also, can I use the PHP SDK as well for logging in instead of the standard code that looks like this:
function facebook(){
auth.authenticateWithFacebook('119914501490688', ['user_about_me', 'user_checkins'], {
onSuccess : function(accessToken, ttl) {
console.log("Success! Here's the access token: " + accessToken);
},
onFailure : function(error) {
console.log("Authentication failed with error: " + error);
},
onComplete : function() {}
});
}
You may need to make sure that Spotify will allow access to the Facebook API endpoints by adding them to your app manifest file. See "Talking to the Outside World" in the tutorial at https://developer.spotify.com/technologies/apps/tutorial/

Facebook graph api Friend Invitations

I have managed to send and retrieve data from facebook for invitatin using
FB.ui({
method: 'apprequests',
to: sendto,
message: 'Win Mega Prizes!',
title: 'Enter in this contest!'
},
function (response) {
if (response && response.request_ids) {
var requests = response.request_ids.join(',');
alert(requests);
window.location.href = "http://www.rmyserver/index.php?invitations="+requests+"&contest_id="+c_id+"&page_id="+page_id+"&user="+from+"&g_id="+g_id+"&sendto="+sendto+"&single=1";
} else {
alert('canceled');
}
PHP
foreach($requests as $request_id) {
// Get the request details using Graph API
echo $request_content = json_decode(file_get_contents("https://graph.facebook.com/$request_id?$app_token"), TRUE);
But i want to use single user app request , as per docs i need to use TO: filed but this doesnt work, request is sent as well i get invitation id but php code used above returns false.
Am i missing something
you can try direct url example given here https://developers.facebook.com/docs/reference/dialogs/requests/#frictionless_requests
that should work fine from php
This works for me:
FB.ui({
method:'apprequests',
message:'I want you to open up your heart a little.',
to:123456798,
// filters:['app_non_users'], // commented: let users send notifications to app users too, probably a good idea to make them revisit?
data:'' // send data what you would like to track, FB will send that back in the URL for us
// #todo send google campaign parameters
});
Moreover you are doing it wrong on PHP side, you would need to redirect the user to a URL where the invitation friend selector would be displayed. There is no way in my knowledge to directly send an invitation to users using PHP.

Facebook Connect - Post Status

I have just about got the Facebook connect working on my site. I am able to log in and PHP has all the users details. I would now like to post a status when a user logs in with the Facebook connect.
I have searched everywhere on how to do this, tried lots of code. And I'm still unable to get anywhere with it. Please be very specific with me as I am new to the Facebook Connect.
It sounds to me like you're looking for the Javascript Facebook SDK. More info about publishing to the stream through javascript can be found at http://developers.facebook.com/docs/reference/javascript/FB.ui.
Have you tried this, from the documentation, on the Publishing section?
You can publish to the Facebook graph
by issuing HTTP POST requests to the
appropriate connection URLs, using an
access token on behalf of the user or
an application access token (for Open
Graph Pages). For example, you can
post a new wall post on Arjun's wall
by issuing a POST request to
https://graph.facebook.com/arjun/feed
EDIT
Snipped from this part of the documentation:
If you have an authenticated user with
the publish_stream permission, and
want to publish a new story to their
feed:
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});

Categories