I found in atlassian documentation this way to get card attachment, but it doesn't work, I get here unauthorized permission requested.
curl -H "Authorization: OAuth oauth_consumer_key="MY_API_KEY", oauth_token="MY_USER_TOKEN"" https://trello.com/1/cards/e7c74l8c5ce3tf6424f95e76/attachments/71cdef9e0734215e3ccbfa82/download/cv.pdf
I tried same with guzzlehttp but same result:
$params_arr = [
'headers' => [
'Authorization' => 'OAuth oauth_consumer_key="' . $api_key . '", oauth_token="' . $token_key . '"'
]
];
$request = $this->request($attachment_url, 'GET', $params_arr);
Does somebody have idea what could be wrong here?
You need to change the URL, replace trello.com with api.trello.com
The result will see something like this: https://api.trello.com/1/cards/e7c74l8c5ce3tf6424f95e76/attachments/71cdef9e0734215e3ccbfa82/download/cv.pdf
Related
I have a problem, I try to use a external API, but the response of that APi is a 401.
1º- I send a request to get the auth token, works fine.
$auth = $this->client->request(
'POST',
'https://api.namebright.com/auth/token',
[
'body' => [
'grant_type' => 'client_credentials',
'client_id' => $clientID,
'client_secret' => $clientSecret,
],
]
);
2º- handler token, i dump the token is loocks fine
$token = sprintf('Bearer %s', $auth->toArray()['access_token']);
3º- I make other request to get the API response, i got a 401.
$response = $this->client->request(
'GET',
'http://api.namebright.com/rest/purchase/availability/google.pt',
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => $token,
],
]
);
I don't know what i'm doing wrong. :(
I checked, for responses on the internet and i don't see the problem.
I Tried to change Authorization to authorization.
I tried to check the token in the postmen, works fine.
I am working on a project using an old version of Guzzle (version 3), I'd like to add a token to the post request, can't find how to do it, I have looked on the doc there is no example for that , here is what I tried so far :
<?php
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => ['Bearer', $token]
));
$req = $client->post($url);
$client->send($req)->getBody(true);
?>
$client->setDefaultOption('auth', array(null, 'Bearer'.$token ))
I keep getting :
Guzzle\Http\Exception\ClientErrorResponseException: Client error response [status code] 400 [reason phrase] Bad Request [url]
I found the solution :
`$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
));`
So instead of array in Authorization I sent a string that contains the token
I am sending a PUT request to an API endpoint I have created. Using jwt, I am able to successfully register and get a token back.
Using Postman, my request(s) work perfectly.
I am using Guzzle within my application to send the PUT request. This is what it looks like:
$client = new \Guzzle\Http\Client('http://foo.mysite.dev/api/');
$uri = 'user/123';
$post_data = array(
'token' => eyJ0eXAiOiJKV1QiLCJhbGc..., // whole token
'name' => 'Name',
'email' => name#email.com,
'suspended' => 1,
);
$data = json_encode($post_data);
$request = $client->put($uri, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
$json = $response->json();
} catch (\Exception $e) {
error_log('Error: Could not update user:');
error_log($e->getResponse()->getBody());
}
When I log the $data variable to see what it looks like, this is what is returned.
error_log(print_r($data, true));
{"token":"eyJ0eXAiOiJKV1QiL...","name":"Name","email":"name#email.com","suspended":1}
Error: Could not suspend user:
{"error":"token_not_provided"}
It seems like all data is getting populated correctly, I am not sure why the system is not finding the token. Running the "same" query through Postman (as a PUT) along with the same params works great.
Any suggestions are greatly appreciated!
The token should be set in the authorization header, not as a post data parameter
$request->addHeader('Authorization', 'Basic eyJ0eXAiOiJKV1QiL...');
I have REST API written in php, i want to test it with phpunit.
I wrote test like this, it works but response body was empty. I tested it with fiddler, it send response body.
Sorry for my english.
class ProgrammerControllerTest extends PHPUnit_Framework_TestCase
{
public function testPOST()
{
// create our http client (Guzzle)
$client = new Guzzle\Http\Client();
$response = $client->post("http://api.loc/v2/", array(
'headers' => "User-Agent: Fiddler\r\n" .
"Host: api.loc\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 34\r\n",
'body' => array('kle' =>'sino','lat' => '41', 'long' => '69'),
));
var_dump($response);
}
}
Can you try with
var_dump($response->getBody()->getContents());
this is my snippet ( it's in Get but it's the same )
$client = new GuzzleHttp\Client();
$response = $client->get('http://192.168.99.100/v1/hello');
var_dump($response->getBody()->getContents());
result:
string(13) "{"bar":"foo"}"
You can use built in method call of Laravel to test your controllers.
$response = $this->call('POST', '/api/v1.0/pages', $parameters);
$data = $response->getData();
Your ProgrammerControllerTest should extend from TestCase
I know Amazon uses Guzzle to test AWS SDK, you can read more info at http://guzzle3.readthedocs.org/testing/unit-testing.html
And also, don't hesitate to dig what others are using (such as Amazon, Facebook etc...), thats why open-source is so great!
I'm developing a webapp in Perl and try to create events in user's Google Calendar with OAuth2. Authentication and requesting calendar-data is working fine, I'm just totally stuck when it comes to sending a POST-request and attaching JSON- or hash-data to it. Are there methods coming with the module for this? The documentation does not point me anywhere here.
I guess LWP would provide ways, but this seems like a lot of overhead.
Here is how i accomplished getting calendar-events so far (as a simple console-app for now):
use Net::OAuth2::Profile::WebServer;
my $auth = Net::OAuth2::Profile::WebServer->new
( name => 'Google Calendar'
, client_id => $id
, client_secret => $secret
, site => 'https://accounts.google.com'
, scope => 'https://www.googleapis.com/auth/calendar'
, authorize_path => '/o/oauth2/auth'
, access_token_path => '/o/oauth2/token'
, redirect_uri => $redirect
);
print $auth->authorize_response->as_string;
my $code=<STDIN>;
my $access_token = $auth->get_access_token($code);
my $response = $access_token->get('https://www.googleapis.com/calendar/v3/calendars/2j6r4iegh2u8o2409jk8k2g838#group.calendar.google.com/events');
$response->is_success
or die "error: " . $response->status_line;
print $response->decoded_content;
Thanks a lot for your time!
Markus
I guess it's time to answer my own question.
To transmit JSON in a POST-request (creating a calendar-event) I ended up using LWP::UserAgent and HTTP::Request. For setting the content-type I first had to create an HTTP::Request-object and set header and data:
my $req = HTTP::Request->new( 'POST', 'https://www.googleapis.com/calendar/v3/calendars/<calendarID>/events' );
$req->header( 'Content-Type' => 'application/json' );
$req->content( "{ 'summary': 'EventName', 'start': { 'dateTime': '2015-02-11T20:48:00+01:00' }, 'end': { 'dateTime': '2015-02-11T22:30:00+01:00' } }" );
Then I created an LWP::UserAgent-object, appended the OAuth2-token to it and let it fire the request:
my $apiUA = LWP::UserAgent->new();
$apiUA->default_header(Authorization => 'Bearer ' . $access_token->access_token() );
my $apiResponse = $apiUA->request( $req );
It's that simple. Would've been a lot nicer to do this all-in-one with Net::OAuth2, though.