Grab current session's cookie with cURL - bash

I'm working on a script that send a POST request to an URL, actually I'm sending the cookies manually in the header.. but how to take it from the current browser session?
I used tcpdump and grep but it's really the wrong choice :,D
Some suggestions?
I wont take them from a file but from the browser session and without enter in the cookie path of the browser

Curl can handle that for you; there is an option to store the cookies in a cookiejar and use those in subsequent requests.
Here is an example from the main curl site, which uses cookies from a file cookies.txt to set some and at the same time stores new cookies in newcookies.txt.
http://curl.haxx.se/docs/httpscripting.html#Cookie_Basics
curl --cookie cookies.txt --cookie-jar newcookies.txt http://www.example.com
When going through a login process, for example, one would reuse the cookies from cookie jar.

I had found a link now broken with an example from snipplr.com. I'll post it here for future references (credits to the owner):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (isset($_COOKIE[session_name()])) {
curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
}
session_write_close();
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'curl error: '.curl_error($ch);
}
curl_close($ch);
session_start();

Related

how to work locally with laravel when third-party api need only production IP to whitelist?

The scenario like this, I deployed my laravel app to heroku. The third-party api needs my production IP for whitelisting in order to use their resources. Heroku doesn't provide ip, I use add on to get ip. It connected ok between my app to third-party now. The problem is that, when I clone my app to local and start to run and test locally, it give error. (Undefined index: host) My code is below
$testparam = 'q='.$q.'&s='.$s;
$quotaguard_env = getenv("QUOTAGUARDSTATIC_URL");
$quotaguard = parse_url($quotaguard_env);
$proxyUrl = $quotaguard['host'].":".$quotaguard['port'];
$proxyAuth = $quotaguard['user'].":".$quotaguard['pass'];
$url = "http://xxxxx.com/api/resource";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxyUrl);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
curl_setopt($ch, CURLOPT_POSTFIELDS,$testparam);
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_
decode($json,TRUE);
dd($array);
Currently, I have to push my file to heroku after I write one or two lines of code in order to see the result. Is there any way to write and test locally?
You need to set the QUOTAGUARDSTATIC_URL environment variable to what is in the QuotaGuard Admin dashboard or in Heroku's heroku config -a APPNAME command.

InvalidAuthenticationToken when uploading file to OneDrive

I am trying to upload a file to OneDrive using the following code:
$graph_url = 'https://graph.microsoft.com/v1.0/me/drive/root:/filename:/createUploadSession';
$file = file_get_contents('logo.jpeg');
$headers = [
'Content-Type: json',
"Cache-Control: no-cache",
"Pragma: no-cache",
"Authorization: bearer EwAQA61DBAAUcSSzoTJJsy+XrnQXgAKO5cj4yc8AAewWnvfTuLAC2gDmYesGiVawqeIpfS8lJjyCyeXQMrKOu92FA0SK12N/87GBTs4ba2LJ5buqmm9gX18by0jPWzZ5qvpc+BdfMC2wUyiMYxh8QH8BJFBctAVgSaKM1KmhA8mAwb2czyw4OeWdupKH7qSPvjRtwBTTxxX8iIO3IeNbpXDghri9MWRGyVIffGf2Oi+RzQm5jbGuDtXrQa4K80m1o8PHcEkdIEXbaAgIE8oBYCVBQ7fyYxA84b0NjghSRbCZDj5rVbo1MDeBv0J4inmPthEbAMcS3fRSBdFs3+SSz+jZwb3SgSp5Q+VXiRs+keDQHspaoSNt4Ho0+pX94WQDZgAACAk/KNYU0yEY4AHihrs84QKvmWWfIOTYCBudQZD7tKp2ZeOpXEI7q3tOTuwHTv7uUmYxkpGswiNSeW2r9DEBdQ5gBtZ/YCXMPhlBLSxxahrl816zsj/UDi2c6dmB+6J1wk60/hm1Ll6vqn8YbtlyQh9hFLpWejo8+Vofh5/kRUfUSl1v7FIP7YIJLvTyNn1gGy9IMnxYdvba9ACGotVpFTAMNdjz6aQwUk4MJjD7csqYVpyMBh8O7bS40dRMjiUkdhXYvA03L/JV818qgo0W0uwJmoqzPJfe7VB+VekqxPCyVT6A86y/OaD1EsTaLwBILG8eS48Odr/qg7azjUsrdryS+rg7wfQEBEL+kIkS5Ne8eTg1/t2aAXFsUukiIBkTYp+Sold4yhqJU2w/cRDZiz9m3GBltjTS+JiIX2Ll0ABbOwmY9pkc6O8/3T0ZhgVuIigfIILTdMGMu9sNvJtVxYL4gbRlNX+gyMBHZHDo2zktdD5l4vDa+TTgn2Fc3CDUli/qLU1UX2SfJ6+B+epmX/wA32cXVtfDPcm605c18LipDoOVGxzf9RVyNe9e3VCFvR9UBmkobodVfE1VhUfzw/s5CHrjzCZ7ZZDt84Hm6VoWIVZLtYueHCuAaEhLa0gWZTKUSwvu3hCDwZUaAg==",
"Content-Length: ".filesize('logo.jpeg'),
];
$ch = curl_init($graph_url);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{}');
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
The result, however, is the always the following error:
"error": {
"code": "InvalidAuthenticationToken",
"message": "CompactToken parsing failed with error code: 8004920A",
"innerError": {
"request-id": "bab6035c-17c5-45ea-99ac-7c809e446ca2",
"date": "2018-12-22T09:42:03"
}
}
First of all, it seems like your token is incorrect. So step 1 is verify the token is correct. You can do that by copying your token and pasting it on the website Microsoft created specifically for this job. https://jwt.ms
Secondly as the microsoft documentation describes you have 2 ways of uploading a file to onedrive.
Direct upload (only for small files) docs
Through a upload session, docs
You're trying to do a direct upload to the CreateUploadSession endpoint.
The first way works (for small files) by uploading the file to the /content endpoint, but that is just a file only upload. So no content headers with json in them.
The second way works by creating an upload sessions (that responds with some json) and then do a second request for uploading the file to the endpoint you got from the url.
The documentation on these things are quite clear, and I think you're using PHP, so it should be easy to mimic the provided curl requests with php.

Yahoo Oauth 2 PHP

I'm working on a project where i need to fetch contacts from yahoo mail. and I'm using Oauth2.0. I'm getting access_token and all things from here https:// api.login.yahoo.com/oauth2/get_token via curl with passing grant_type and all required parameters which is fine. but the problem is refresh_token and xoauth_yahoo_guid from yahoo I'm redirecting to this url https://social.yahooapis.com/v1/user/{xoauth_yahoo_guid}/contacts?format=json. I'm getting this output:-
{"error":{"#lang":"en-US","#uri":"http://yahoo.com","description":"Not Authorized - Either YT cookies or a valid OAuth token must be passed for authorization","detail":"Not Authorized - Either YT cookies or a valid OAuth token must be passed for authorization"}}
However I'm working in codeigniter and i need this task to be done in ajax or javascript But I've tried a lot but unfortunately nothing worked out for me. Please help me to get rid out of this problem.
Thanks.
Finally I got the answer. I just have to send the header
$headers= array(
'Authorization: Bearer '.< access_token >,
'Accept: application/json',
'Content-Type: application/json'
);
with following Code:-
$ch = curl_init();
$url = "https://social.yahooapis.com/v1/user/me/contacts?format=json";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$output3 = curl_exec ($ch);
curl_close ($ch); // close curl handle
$finalresult = json_decode($output3);
echo'<pre>';print_r($finalresult);

HTTP Session from service cloud using PHP code

Is it possible to send custom XML messages from oracle service cloud while maintaining HTTP session?
So far I've managed to send a single message using cURL:
<?php
use \Rightnow\Connect\v1_2 as RNCPHP;
use \Rightnow\CPM\v1 as RNCPM;
$url1 = "";
$startInterviewHeaders = array("SOAPAction: http://oracle");
$startInterview;
// session
//$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
if(!function_exists("\curl_init")){
\load_curl();
echo "curl loaded";
} else {
echo "curl already exists ";
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $startInterview);
curl_setopt($ch, CURLOPT_HTTPHEADER, $startInterviewHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$startInterviewresponse = curl_exec($ch);
//echo $response;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno= curl_errno($ch);
echo "</br> HTTP status: " . $http_status . "</br> cURL error: " .$curl_errno . "</br>";
curl_close($ch); // close cURL
echo $startInterviewresponse;
?>
EDIT: Code above sends a single message and gets a response, but when I try to maintain HTTP session with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE, second cURL message response complains that there is no active session.
sessions.com.oracle.determinations.server.exceptions.NoActiveInterviewExceptionaction "Investigate" can not be performed without an active interview
Code I used to maintain session (worked when testing outside Rightnow environment)
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems
Thanks!
Your code sample is a Custom Process Model. CPMs do not allow persistence and will be closed once the CPM completes. You can run multiple curl calls from one CPM, but that is not recommended; you should use an integration middleware if you need multiple calls per CPM.
As long as you're running this code from an asynchronous CPM (synchronous CPMs don't expose curl, so that should be the case here), then an error on the OSvC side is likely an issue connecting to your "local test server", which is almost always not exposed to the public internet in an enterprise environment. Therefore, your "crash" is likely a connection error.
Per #drew010, you need to include your error in the context of this question as well.

Posting to a user's Yammer feed using Open Graph

I successfully posted an activity to the activity feed using the activity API and open graph. However, I do not see a post on the actor's Yammer feed when I click the user's name. I assumed that the activity API would post there as well because of what I read in the documentation:
This activity will appear in the Yammer Activity Stream Ticker (shown above) and will be delivered to the actor, the actor’s followers and the people specified in the “users” list
Here is the data I'm posting to the API. You can see that I'm also adding the actor as one of the 'users' who should get the post (look right after "private"=>"false"). Still no good.
$text=array("activity"=>
array("actor"=>
array("name"=>$res->user->full_name,
"email"=>$res->user->contact->email_addresses[0]->address),
"action"=>"domain:action",
"object"=>array("url"=>"https://website.com","type"=>"domain:object","title"=>"post"),
"private"=>"false",
"users"=>array("email"=>$res->user->contact->email_addresses[0]->address,"name"=>$res->user->full_name),
"message"=>"my first post"));
$text is then json_encoded. Here is the curl call:
$url="https://www.yammer.com/api/v1/activity.json";
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$jtext);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$accToken,'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
Here is the response to the above curl request:
stdClass Object
(
[action_id] => xxxxxxxxx
[object_id] => xxxxxxxxxxxxxxx
[actor_id] => xxxxxxxxxx
)
The Activity Stream Post is working fine as far as I can see, just wondering what code I'm missing (or API call) to post on the user's feed. This is written in PHP and needs to stay in PHP. Thanks in advance for the help.
The solution to my problem was that I wasn't using the messages API for posting to the user's wall.Here is working code based on the current API specification. I hope this helps someone else.
NOTE- the message has to be called 'body' and you need to set the 'Content-Type: application/json' in the header:
$text=array("body"=>"This is a test post");
$jtext=json_encode($text);
$url="https://www.yammer.com/api/v1/messages.json";
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$jtext);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$accToken,'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);

Resources