I'm trying to flag post-id as job discussion with OAuth PUT :
$post_id = "G-34343-example";
$cmd = "http://api.linkedin.com/v1/posts/{$post_id}/category/code";
$xml = "<?xml version='1.0' encoding='UTF-8'?><code>job</code>";
$this->oauthc->fetch($cmd, $xml, OAUTH_HTTP_METHOD_PUT, array("Content-Type" => "application/xml"));
But I'm getting an error:
Someone know what does it mean?
com.linkedin.common.leo.core.UnauthorizedActionException: Unauthorized change of forum type
The problem lies here:
<?php
$post id = "G-34343-example";
?>
There's a space in the variable name. Use an underscore instead of a whitespace to make it work.
(This: $cmd = "http://api.linkedin.com/v1/posts/{$post_id}/category/code"; will contain a blank spot where the post id should be.)
Is the user you are connecting with using the API authorized to flag posts in the target group? Have you tried logging in to LinkedIn directly as this user, going to the target forum and attempted to flag a post?
When creating a group post, it's URL is returned in the "location" header. For some reason, the URL from the location header is using http and not https.
If you're using OAuth2, LinkedIn returns a 401 if you try to access the API with http instead of htttps. So if you're getting a 401 response, check that you're using https.
So the code should be something like:
$url = $response->headers['location'] . '/category/code';
$url = str_replace('http://', 'https://', $post_url);
// ...
Related
I'm making a request to retrieve a JSON file to a server at a particular secure DocuSign uri. However, unless I put in the authorization information (which I do have), I am unable to have the file returned.
<?php
$json = file_get_contents("https://example.docusign.com/sensitiveIDs/moreID");
echo $json
?>
Where would I put in authorization information for the specific server/username/password/other info needed to access the particular DocuSign server using a method like this in PHP? Is there a better method to use for this scenario in PHP?
It depends on how the authorization is implemented. If its basic or digest HTTP authentication then specify it in the URL:
file_get_contents("https://$USER:$PASSWORD#example.docusign.com/sensitiveIDs/moreID");
Cookie based authentication is a lot more difficult (and probably easier to use Curl or even a more complex system like Guzzle. If its oauth2, then you probably want an oauth2 library.
Your call needs to include authentication to make the GET call to retrieve the file.
If your app is initiated by a human use Oauth to retrieve access and refresh tokens. Then included the access token with the GET request.
If your app is a "system app" that wants to autonomously retrieve the file, then you should authenticate by using X-DocuSign-Authentication -- include the following header in your HTTPS request. Since the request is HTTPS, the content is encrypted on the wire:
X-DocuSign-Authentication: <DocuSignCredentials><Username>{name}</Username><Password>{password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>
Replace {name} with your email address (no braces), etc.
The bottom line is that you can't use the file_get_contents Php method. Instead, you'd do something like the following:
Use https://github.com/rmccue/Requests or a similar library to help with the https request. (http is not allowed due to security issues.)
(untested code)
$url = $base_url . $the_url_section_for_this_call
$headers = array('X-DocuSign-Authentication' =>
'<DocuSignCredentials><Username>your_name</Username><Password>your_password</Password><IntegratorKey>your_integrator_key</IntegratorKey></DocuSignCredentials>');
$request = Requests::get($url, $headers);
# Check that the call succeeded (either 200 or 201 depending on the method
$status_code = $request->status_code;
if ($status_code != 200 && $status_code != 201) {
throw new Exception('Problem while calling DocuSign');
}
$json = $request->body;
So I am trying to get some information from a JSON file. But for some reason there is no actual response with the information that I need.
This is the code im running.
$json_object = file_get_contents('http://steamcommunity.com/profiles/'.$steamID.'/inventory/json/730/2');
$json_decoded = json_decode($json_object);
echo $json_decoded->success;
I get the steamID from a cookie. But if you want to see how the JSON looks, then you can check out this link:
http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2
The problem is that the URL you are checking is redirected to another URL, file_get_contents() doesn't support following redirects AFAIK, so you should better use cURL
Check this answer https://stackoverflow.com/a/4324018/5658508
the problem is like the others tell you is in this link http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2
i just monitor the response, so when you visit the link you get a 302 Moved Temporarily http response to the new link which is http://steamcommunity.com/id/SANDISS/inventory/json/730/2
you can try to get contents using file_get_contents if you have the link not just a steam id, or use curl library
Im trying to scrape information from the site http://steamstat.us - The thing i want to get is the status and such from the site.
Im currently only using this code:
<?php
$homepage = file_get_contents('http://www.steamstat.us/');
echo $homepage;
?>
The problem I have here is that "Normal (16h)" and the rest just returns 3 dots.
Cant figure what the problem should be.
Anyone have any clue?
EDIT
This is now fixed.
I solved the problem as followed:
<?php
$opts = array('http' => array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$json_url = file_get_contents('https://crowbar.steamdb.info/Barney', FALSE, $context);
$data = json_decode($json_url);
?>
Its a https protocol which is not easy to scrap. Thought the website allows it as the headers sent for "access-control-allow-origin" are marked as * which means the content can be requested by any other site.
You are not receiving the content becasue Normal (16h) is not yet populated on page load. Its coming from ajax.
The HTML source says <span class="status" id="repo">…</span>. You are receiving these three dots inside span tag in file_get_contents.
The only way to do it is to look for the ajax call in the network log and then use file_get_contents in that URL called by ajax.
I am working with a third party service that requires me to authenticate through OAuth1 to make requests. I can successfully authenticate and get data back with most of their calls using GET, however some calls they require POST and this is where the problem lies. To authenticate I am using the below:
$oauth = new OAuth(MY_KEY,MY_SECRET);
$oauth->setNonce(rand());
$oauth->setToken('','');
Then for a GET call I am doing something like below:
$array = array(
'partnerId'=>'1234'
);
$call = $oauth->fetch("https://domain.co.uk/api/getInfo/",$array);
$data = $oauth->getLastResponse();
This all works perfectly, and I can print out the $data
However with POST calls:
$oauth = new OAuth(MY_KEY,MY_SECRET);
$oauth->setNonce(rand());
$oauth->setToken('','');
$oauth->enableDebug();
$oauth->setAuthType(OAUTH_AUTH_TYPE_AUTHORIZATION);
$array = array(
'rid' => "$restaurantId",
'datetime' => "$datetime",
'partysize' => $covers,
'timesecurityID' => "$securityId",
'resultskey' => "$resultskey"
);
$call = $oauth->fetch("https://domain.co.uk/api/booking/?pid=1234&st=0",$array,OAUTH_HTTP_METHOD_POST);
$data = $oauth->getLastResponse();
I keep getting the error: Invalid Consumer Signature
Speaking to their tech guy he suggested
Your sbs value indicates that you’re signing with all of the POST
parameters, whereas you only need to sign with the query string. In
this case it would be “pid=1234&st=0”. Unfortunately, I’m not
familiar with the PHP libs and don’t have any recommendations on how
to alter the behavior.
and also mentioned common previous problems with a PHP implementation are:
The PHP HTTP lib will drop the query string once the method is
changed from GET to POST.
The PHP oAuth lib will use the post data
to sign the request rather than the query string or maybe both.
If I dump out the headers I get:
[sbs] => POST&https%3A%2F%2Fdomain.co.uk%2Fapi%2Fbooking%2F&datetime%3D2013-02-21T10%253A30%253A00%26oauth_consumer_key%3DMySiteV3TT%26oauth_nonce%3D1213111151%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1360835965%26oauth_token%3D%26oauth_version%3D1.0%26partysize%3D2%26pid%3D1531%26resultskey%3DfoqgEnYK%25252bIzRd6BV3T8eGQ%25253d%25253d%26rid%3D31852%26st%3D0%26timesecurityID%3D349367809
[headers_sent] => POST /api/booking/?pid=1234&st=0 HTTP/1.1
It looks like it is sending the OAuth data with the rest of the post, I just want this sent in the Authorization header (which it is also sending)
Authorization: OAuth oauth_consumer_key="MySite",oauth_signature_method="HMAC-SHA1",oauth_nonce="1772854358",oauth_timestamp="1360768712",oauth_version="1.0",oauth_token="",oauth_signature="2%2B7xb%2BJ5cdbUDC5UHfsdfsNpFM1pE%3D"
So I think I need to strip the OAuth data from the post request but keep it as a Authorization Header but just can't find the magic way to do that!
I've seen this. In fetch(), try urlencoding your $array and passing it in as a string of the form:
rid=[restaurantId]&datetime=[datetime]&...
Also give the header (final parameter of fetch()):
Content-Type: application/x-www-form-urlencoded
I am trying to get gmail contacts using Google contacts api. For this i am using OAuth 2.0. For this i read their guide for using OAuth 2.0 here. Broadly i followed these steps
Register my app with google and get a client id and client secret and registered my redirect uri with them
now i first created a file called sample.php on which if the user clicks on get contacts he is redirected to google confirmation page
now google asks for confirmation and if the user agrees to provide its contact details then he is redirected to the redirect uri with a code.
now i extract the code and make a post request to get the OAuth token and if i get the token i make a request for the contacts.
The code for the sample.php looks like this
<?php
$client_id="my client id";
$redirect_uri="http://localhost/htdocs/contacts/redirect.php";
echo <<<doc
<html>
<body>
<a href="http://accounts.google.com/o/oauth2/auth?client_id=$client_id&redirect_uri=$redirect_uri&scope=https://www.google.com/m8/feeds/&response_type=code">
get contacts
</a>
doc;
echo <<<doc
</body>
</html>
doc;
?>
the code for the redirect.php looks like this
<?php
$client_id="my client id";
$client_sec="my client secret ";
$redirect_uri="http://localhost/htdocs/contacts/redirect.php";
$code=$_GET['code'];
$post="code=$code&client_id=$client_id&client_secret=$client_sec&redirect_uri=$redirect_uri&grant_type=authorization_code";
$post=urlencode($post);
//the following curl request is made to get the authorization token
$ch=curl_init();
$url="https://accounts.google.com/o/oauth2/token";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$json=curl_exec($ch);
curl_close($ch);
echo $json; // this is showing Required parameter is missing: grant_type Error 400
$res=json_decode($json,true);
print_r($res); // this is just for debugging
$token=$res['access_token'];
echo $token; // this is just for debugging
$url="https://www.google.com/m8/feeds/contacts/default/full?oauth_token=$token";
//the following curl request is made to get the contacts
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
$xml=curl_exec($ch);
echo $xml;
curl_close($ch);
$dom= new DOMDocument();
$dom->loadXML($xml);
$xpath=new DOMXPath($dom);
$path='//gd:email';
$nodes=$xpath->query($path);
echo $nodes->length."<br />";
foreach($nodes as $node)
{
$email=$node->attributes->getNamedItem('address')->nodeValue;
echo $email."<br />";
}
?>
now the problem is that step 4 fails. It goes well till the confirmation step and when i click Allow access i am redirected to my redirect uri but its saying
Required parameter is missing: grant_type Error 400
now i dont understand this because i am providing the grant_type parameter. i looked in firebug and found out that a post request is being made but not the one which i intended. the post request that is being made is this
https://accounts.google.com/o/oauth2/approval?xsrfsign=AC9jObYAAAAATkfm3uvx0sfsW7WVmB_FeRqVPLjDcTLz and the status says 302 moved Temporarily. I dont understand whats going on. have they changed something in the OAuth workflow? Need some help
Edit
As suggested bu Ikke i removed the space before grant_type and it worked. Now i can get the access token but still i am unable to get the contacts.(i get an error as Empty string supplied as input in htdocs\contacts\redirect.php on line 35 and line 35 refers to $dom->loadXML($xml); so it seems that the get request is not being made) Moreover i am not able to see the post request being made in firebug (but surely its being made because i get the access token as the response). i also dont see the get request in firebug which i make after post request whats wrong?
Update:
the problem was that the request was on https and i was not using the appropriate headers for https. I used the correct headers and it worked.Problem solved? Maybe. Because i still dont understand why i am unable to see those get and post requests in firebug's' net` tab
There is a space between the & and the grant_type. That could cause google not to recognize the parameter.
"Because i still dont understand why i am unable to see those get and post requests in firebug's'net` tab"
I assume since you are requesting stuff with curl, it has nothing to do with firefox, since curl is a server-side library, therefore POSTs made by curl do not go through browser and firebug cannot see them.
You're passing an already urlencoded string to PHP's urlencode():
$post="code=$code&client_id=$client_id&client_secret=$client_sec&redirect_uri=$redirect_uri&grant_type=authorization_code";
$post=urlencode($post);
This messes up the value, Google's server can't properly parse it and therefore reports a missing parameter. Remove the $post=urlencode($post); line and see if it works better.