I am trying to implement the authentication as specified here : https://api.thermosmart.com/apidoc/
I get through the first steps and get an authorization code back. I need to exchange that for an acces_token. The step in curl is specified as:
# 4. Exchange authorization code for Access token (read out the code from the previous response)
curl -k -u client123:client-secret -b cookie.txt -vd "grant_type=authorization_code&code=HPOfdlCbiZ1tI4Lv&redirect_uri=..." "url for token"
My code in PHP:
$curl = curl_init( 'url for token' );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
$userpwd = $oauth2_client_id.":".$oauth2_secret;
curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'redirect_uri' => $oauth2_redirect,
'code' => $code, // The code from the previous request
'grant_type' => 'authorization_code'
) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$auth = curl_exec( $curl );
}
$secret = json_decode($auth);
$access_key = $secret->access_token;
echo "OAuth2 server provided access token: " . $access_key;
Any help on this will be much appreciated.
Have struggled with the same question (for Qt Quick application, but maybe it can help you)...
(Asking for help resulted in quick response from ThermoSmart...
My code to exchange access token for authorization code:
req.open("POST", "https://api.thermosmart.com/oauth2/token", true, client_id, client_secret);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send("grant_type=authorization_code"+"&code="+ authorization_code + "&redirect_uri=" + redirect_uri);
Related
I am using get/post request to generate embed token of the powerbi report in php and i successfully generated the access token by following the example given in this link https://community.powerbi.com/t5/Developer/How-To-Get-embed-token-using-Get-Post-only/td-p/294475 but when i used this access token to generate embed token for me it returns empty array in response.This is my code
$headers = array(
"Authorization: Bearer <acesstoken generated>"
);
$url = 'https://api.powerbi.com/v1.0/myorg/groups/<group-id>/reports/<report-id>/GenerateToken';
$post_params = array(
'accessLevel' => 'View',
'datasetId'=>'<dataset-id>'
);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo $response;
Any help would be appreciated thanks.
After the hardwork of 1 day I finally figured out i was using application id in the resource param for generating access token instead of this https://analysis.windows.net/powerbi/api link. So when i used this link ,the problem is solved and generated the embed token successfully.
I am using the following code to request a token from a IdentityServer, which uses OpenID protocol:
$curl = curl_init( 'https://remoteserver.com/connect/token' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$code = $_GET['code']; // The code from the previous request
$redirect_uri = 'http://mycalldomain.com/test.php';
curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
) );
curl_setopt( $curl, CURLOPT_USERPWD,
"MYCLIENTID" . ":" .
"MYCLIENTSECRET");
$auth = curl_exec( $curl );
print '$auth = ';print_r($auth); // to see the error
$secret = json_decode($auth);
$access_key = $secret->access_token;
Is outputing the following error:
$auth = {"ErrorMessage":"Unsupported Mediatype"}
Can someone guide on this please?
You should provide a Content-Type HTTP header that the resource you're POSTing things accepts, by adding something like this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
That will make it JSON (as an example!), and your output data (CURLOPT_POSTFIELDS) must correspond to the Content-Type you choose.
Currently, the Content-Type is "multipart/form-data", as per the PHP documentation:
If value is an array, the Content-Type header will be set to multipart/form-data.
If you want to use the Content-Type "application/x-www-form-urlencoded", then in addition to setting that as the Content-Type, you have to give CURLOPT_POSTFIELDS in that format too. Being a web development language, PHP has a built-in function http_build_query for encoding arrays in that format:
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
)));
I'm trying to use php and cURL to make requests to the Fitbit oauth 2.0 api. I can get my authorisation code but cannot manage to exchange the code for a token. The Fitbit api docs say (https://dev.fitbit.com/docs/oauth2/#access-token-request) that I need to post code, client id, redirect uri and grant type set to 'authorization_code'.
Howver, I keep getting an error when I print the response.
"errorType":"unsupported_grant_type","message":"The authorization grant_type is not supported. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}
For the life of me I cannot work out what I am doing wrong with the below code. Any suggestions?
$code = $_GET['code'];
$url = 'https://api.fitbit.com/oauth2/token';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'code=' . $code . '&' .
'client_id=' . $oauth2_client_id . '&' .
'redirect_uri=' . $oauth2_redirect . '&' .
'grant_type=authorization_code'
)
);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '. base64_encode($oauth2_client_id.':'.$oauth2_secret),
'Content-Type: application/x-www-form-urlencoded'
));
$response = curl_exec($curl);
print_r($response);
You're concatenating the POST arguments in to a single string and then include it in an array but they should be individually presented; that can be done in as follows:
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'code' => $code,
'client_id' => $oauth2_client_id,
'redirect_uri' => $oauth2_redirect,
'grant_type' => 'authorization_code'
)));
See: curl POST format for CURLOPT_POSTFIELDS
For Previous question about Azure account, I can create an app using azure account.
Now I can get auth code from below url:
Auth_code
From Auth_code we can get the access token by:
$auth_code = $_GET['code'];
$result = access($auth_code);
function access($auth_code){
$redirectUri = 'https://XXXX /authorize.php';
$token_request_data = array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => $redirectUri,
"client_id" => "client_id",
"client_secret" => "client_secret",
"resource" =>"resource" (From manifest in azure)
);
$token_request_body = http_build_query ( $token_request_data );
$curl = curl_init ( 'https://login.windows.net/common/oauth2/token' );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl, CURLOPT_POST, true );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $token_request_body );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec ( $curl );
$res = json_decode($response);
curl_close ( $curl );
Now I'm trying to access the web api using that access_token,I couldn't get the result.
For example:
$authHeader = 'Authorization:Bearer access_toke';
$ch = curl_init();
$url = 'https://domain/api/data/v8.0/contacts';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json'));
$result = curl_exec($curl);
echo "<pre>";print_r($result);exit;
curl_close($curl);
I'm getting empty response. Now I have to know how to access the web API using access token.
When I try to run manually https://domain/api/data/v8.0/contacts, I can get all contacts in my crm.But when I try to access it by access_token using php,it returns empty.
Web api reference url : reference for web api url
Refer to the guide Compose HTTP requests and handle errors, as the requirements shown at HTTP headers section:
Every request should include the Accept header value of application/json, even when no response body is expected.
And there are supernumerary blank in your PHP script at curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json'));
You can remove the blanks in Content-Type and try again.
By the way, you can leverage Fiddler to capture the requests from your PHP client. We you get the response body content and request status code via the tool. We can match the status code with the list at https://msdn.microsoft.com/en-us/library/gg334391.aspx#bkmk_statusCodes. If the code is always 200 without response body, you may check your code in web api.
Is there any good tutorial that teaches how to post on user's wall with API? This is the flow:
The user will come on my website and click on post to facebook button at the end of article.
He is shown sign in dialog from facebook, after sign in he will give permission to my application to post on his wall on his behalf.
After authorization, his shared link will be posted on his wall.
Upon future shares, he will not be asked for permissions since he has already given permission to post on his behalf. So in future when he clicks 'Post to Facebook' button under the article then that item will be posted to his wall without opening facebook login dialog.
I have searched a lot on tutorials but have not found any that meets my requirement.
I am very new to facebook API and have not worked with it before.
Any suggestions or link to tutorials?
Thank You!
I've code to help you to post status with an image to an user's timeline.
After user has given permission to your app , you might have received a query string parameter called 'code' , using this code , we can post to user's timeline.
$code = #$_REQUEST["code"];
if(!empty($code))
{
// Start : Get live user access token ------------------------------------------ //
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . FACEBOOK_APP_ID
. "&redirect_uri=" . urlencode( FACEBOOK_POST_LOGIN_URL)
. "&client_secret=" . FACEBOOK_APP_SECRECT
. "&code=" . $code;
$token = $this->get_app_access_token(FACEBOOK_APP_ID,FACEBOOK_APP_SECRECT,$token_url);
$access_token = $token;
// End : Get live user access token ------------------------------------------- //
// Start : Create album ------------------------------------------------------ //
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$uri = 'https://graph.facebook.com/albums?access_token='.$access_token;
$post_fields = array('name' => trim( FACEBOOK_ALBUM_NAME ));
$curl = curl_init( $uri );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );
$raw_data = curl_exec($curl);
curl_close( $curl );
$created_album_id = json_decode( $raw_data, $assoc = TRUE );
// End : Create album ---------------------------------------------------------- //
$facebook_share_image_url = FACEBOOK_SHARE_IMAGE_PATH;
$facebook_status_text = 'The status text';
$graph_url= "https://graph.facebook.com/me/photos";
$postData = "url=" . urlencode($facebook_share_image_url)
. "&message=" . urlencode($facebook_status_text)
. "&access_token=" .$access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
// End : Add photos ------------------------------------------------------------- //
}
and the function to get app access token
function get_app_access_token($app_id, $secret,$token_url)
{
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => FACEBOOK_APP_ID,
"redirect_uri" => urlencode(FACEBOOK_POST_LOGIN_URL),
"client_secret" => FACEBOOK_APP_SECRECT
);
$a1 = $this->file_get_contents_curl($token_params,$token_url);
$a2 = str_replace("access_token=","",$a1);
$a3 = explode("&expires",$a2);
return $a3[0];
}
The other function access graph url
function file_get_contents_curl($params,$url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$headers = array(
"Cache-Control: no-cache",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
Hope it helps..!!
You can use simple facebook sharer URL:
https://www.facebook.com/sharer/sharer.php?u=YOURPAGEURL
Where YOURPAGEURL is the page URL which you want to share.. Hope this helps.
you have to permission pubish_stream on your app and then try this using curl:-
Also you need a user access token then send access token with curl call
$attachment = array(
'access_token' => $access_token,
'message' => 'i m success to using graph api for post wall',
'name' => 'Wall Post using graph api',
'link' => 'www.mysite.com',
'description' => 'Using the Graph API, any Facebook users Wall feed may be accessed by using this URL:',
'picture'=>'http://example.com/images/noimage.png'
);
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result)
For more :- Graph API new feed post object-Attachment not showing
Post to a Facebook user's wall with cURL PHP