Developers of the Drive SDK - or generally, the OAuth2.0 PHP client library!
In the apiClient.php there is a setAccessToken function:
public function setAccessToken($accessToken) {
if ($accessToken == null || 'null' == $accessToken) {
$accessToken = null;
}
self::$auth->setAccessToken($accessToken);
}
The #param of this function is something like this:
{"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
"expires_in":3600, "id_token":"TOKEN", "created":1320790426}
Why you name this parameter $accessToken if the access token is just a part of this JSON encoded string ??
It's very misleading i think.
When we go deeper and look at: $auth->setAccessToken($accessToken); in apiOAuth2.php
we see:
public function setAccessToken($accessToken) {
$accessToken = json_decode($accessToken, true);
if ($accessToken == null) {
throw new apiAuthException('Could not json decode the access token');
}
if (! isset($accessToken['access_token'])) {
throw new apiAuthException("Invalid token format");
}
$this->accessToken = $accessToken;
}
Look at the second if: $accessToken['access_token']. Whats the point of this? Access token inside an accessToken ?? :)
You should name the $accessToken parameter (the whole JSON string) of these functions to something else like $credentials or whatever because it's a little bit blurry... but tell me if I'm wrong.
As some of the comments mention I would post this either on the Google APIs PHP client library form: https://groups.google.com/forum/?fromgroups#!forum/google-api-php-client
Or file an issue on their issue tracker: http://code.google.com/p/google-api-php-client/issues/list
This will make sure that the engineers working on the PHP client library will be aware of this.
And you are right this makes perfect sense :)
Related
I have a Demo Server replying JSON objects only to the client request.
I am planning to use QNetworkAccessManager as the client, this is what I did.
I defined a lambda function handling Server reply
std::function<void(QNetworkReply*)> processReplyLB = [&](QNetworkReply *reply){
static int cnt = 0;
std::cout<<"--------------------"<<(++cnt)<<"---------------------"<<std::endl;
QList<QByteArray> headerList = reply->rawHeaderList();
foreach (QByteArray header, headerList) {
std::cout<<header.constData()<<" - "<<reply->rawHeader(header).constData()<<std::endl;
}
processResult = false;
if(reply->error()){
std::cout<<"REPLY ERROR"<<std::endl;
std::cout<<reply->errorString().toUtf8().constData()<<std::endl;
} else {
QString value = reply->readAll();
std::cout<<"value = "<<value.toUtf8().constData()<<std::endl;
QJsonDocument doc = QJsonDocument::fromJson(value.toUtf8());
if(doc.isNull()){
std::cout<<"JSON document is null"<<std::endl;
}else if(doc.isEmpty()){
std::cout<<"JSON document is empty"<<std::endl;
} else if(!doc.isObject()){
std::cout<<"JSON document is not an object"<<std::endl;
} else {
QJsonObject obj = doc.object();
QString responseStr = obj.value("result").toString();
processResult = (responseStr == "ok");
if(obj.contains("message")){
QJsonValue messageValue = obj.value("message");
std::cout<<messageValue.toString().toUtf8().constData()<<std::endl;
}
}
}
reply->deleteLater();
std::cout<<"--------------------"<<(cnt)<<"---------------------"<<std::endl;
};
and I connected this lambda slot to QNetworkAccessManager in two functions used for
check if client session does exist on Server(sends a get request).
login using id and password (send a post request with parameters).
in main function, if I invoke checkSession() or login() respectively, the result is fine. but if I try to call
login();
checkSession();
in sequence, then I will get lambda invoked four times with checkSession() result came as the first, following by a null json, login json result and finally another null json.
I know QNetworkAccessManager works asynchronously, EventLoop can solve this problem, but it is not applicable in real development mode due to I am writing a client background service component.
So how can we design this client so that I can make sure login result is processed before checkSession?
BTW, I used Java Servlet for Server without asynchronous. they are just trivial doGet and doPost processes.
i have installed rtMedia on my web-site(which i have already installed buddypress) and now i’m trying to write an android application using the api of rtMedia but when i try to do a request like http://example.com/rtmedia_api/wp_login/?username=USER&password=PASS it display me “NOT FOUND”.
I have also tryed using the endpoint, like this http://example.com/wp-admin/admin-ajax.php/rtmedia_api/wp_login/?username=USER&password=PASS in this case it display me “0″.
I don’t understand how to use this api with json code to do json request.
I use also other plugin which have api and to perform a request i just need to type something like this http://example.com/api/?json=get_recent_posts/?cookie=COOKIE
Why it dosen’t work whith rtMedia api??
(sorry for my bad english :D )
This isn't the way rtMedia API will work.
Please check this doc about rtMedia JSON API.
For any queries regarding rtMedia, you can create a support request here
The rtmedia api plugin requires you to submit it via POST request (even though the confusing 'documentation' tells you otherwise with some functions). The easiest way to go about this is to create a form using the method="POST" and action="yourhost.domain/wordpress/wp-admin/admin-ajax.php". Another way to go about this is by using javascript (which I presume you will be using) and creating a formdata object like so: var formData_rtmedia = new FormData(); and appending (for example wp_login) the strings of your input field like this:
// _(id) is a function that returns document.getElementById(id)
var data1 = _('wp_login_input1').value;
var data2 = _('wp_login_input2').value;
if(data1 != undefined && data1 != '') {formData_rtmedia.append('username', data1);}
if(data1 != undefined && data1 != '') {formData_rtmedia.append('password', data2);}
Using the if dataX not equals undefined and not equals empty string check ensures the fields that are optional can be empty and won't populate the postdata object with empty or null values. The object can be sent using ajax (aka an XMLHttpRequest object) like this:
var xhr;
function ajax(method, url, async, formData) {
if(window.XMLHttpRequest) {xhr = new XMLHttpRequest();}
else {xhr = new ActiveXObject('Microsoft.XMLHTTP');}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status != 200) {/* handle error */}
else {/* handle response */}
} else {
// display ajax loader or do something fancy
}
}
xhr.open(method, url, async);
xhr.send(formData);
}
and calling it in your rtmedia javascript file like so: ajax('POST', url, true, formData_rtmedia);
I'm calling CalendarApp.getAllCalendars()
on the server via a call on the client
/*- server code-*/
function getCalendars(){
return CalendarApp.getAllCalendars();
}
/*-*/
/*-client code-*/
google.script.run.withSuccessHandler(getCalendarsHandler).getCalendars();
getCalendarsHandler=function(cals){
console.log(JSON.stringify(cals));
};
/*-*/
console shows...
[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
getCalendarsHandler receives an array of the correct length (i.e. the number of calendars I have access too)
but each element in that array is null,
could someone tell me what I'm doing wrong ?
Thanks
later that day.....
on further investigation it looks like I'll have to build the structure on the server before passing it to the client. I was expecting something similar to gapi.client.calendar.calendarList.list(); but it looks like I'll have to build my own - something like....
function getCalendars()
{
var cal,i,resp;
resp=[];
cal=CalendarApp.getAllCalendars();
for(i=0;i<cal.length;i++)
{
resp[i]={
"name":cal[i].getName(),
"id":cal[i].getId()
...
}
}
return resp;
}
later that day.....
on further investigation it looks like I'll have to build the structure on the server before passing it to the client. I was expecting something similar to gapi.client.calendar.calendarList.list(); but it looks like I'll have to build my own - something like....
function getCalendars()
{
var cal,i,resp;
resp=[];
cal=CalendarApp.getAllCalendars();
for(i=0;i<cal.length;i++)
{
resp[i]={
"name":cal[i].getName(),
"id":cal[i].getId()
...
}
}
return resp;
}
I am trying to send some data from Dart to PHP...
This is the code i am using to send the data from Dart:
button.onClick.listen((e) {
var req = new HttpRequest();
req.onReadyStateChange.listen((HttpRequestProgressEvent e) {
if (req.readyState == HttpRequest.DONE) {
print('Data submitted!');
}
});
req.open('POST', form.action);
req.send('hello from dart');
});
In my PHP file I am trying to use the string i have send from dart, but count($_POST) returns 0. $_POST seems to be empty...
Dart code DOES trigger the php script and 'Data submitted' is printed...
This is actually related to your PHP configuration. You can access the POST'd data with PHP's reserved variable: $HTTP_RAW_POST_DATA However the preferred method is to use php://input
I am very new to Dart, but you can use FormData in the send. So a quick and dirty way could be.
var data_form = new FormData(query('#My_form'));
button.onClick.listen((e){
var request = new HttpRequest():
request.open('POST', 'http://Localhost/form_data.php');
request.send(data_form);
I'm working on a RESTful application using CodeIgniter and I'm unable to access my POST'd json data in my controller.
I'm posting the json via cURL on my local machine, while the app is being developed on a remote server.
Here is the controller code in question:
class Products extends CI_Controller
{
public function __construct()
{
$this->load->model(products_model);
}
public function index($id = FALSE)
{
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
// fetch product data
$product_data = $this->products_model->get_products($id)
// set appropriate header, output json
$this->output
->set_content_type(application/json)
->set_output(json_encode($product_data));
}
elseif($_SERVER['REQUEST_METHOD'] == 'POST')
{
// debugging for now, just dump the post data
var_dump($this->input->post());
}
}
}
The GET action is working well enough, and returning the appropriate data when requested from a browser, or via a cURL request. However, when attempting to POST json data via cURL I consistently get bool(FALSE) returned from the POST section of the index function. Here is the cURL request I'm making:
curl -X POST -d #product.json mydomain.com/restfulservice/products
Also, here is the contents of the product.json file:
{"id":"240",
"name":"4 x 6 Print",
"cost":"1.5900",
"minResolution":401,
"unitOfMeasure":"in",
"dimX":0,
"dimY":0,
"height":4,
"width":6}
I've made another POST via cURL, excluding the json data and passing something like this:
curl -X POST -d '&this=that' mydomain.com/restfulservice/products
Which returns
array(1) {
["this"]=>
string(4) "that"
}
What gives? Something with the json? It's valid. I've turned off the global CSRF and XSS in application/config/config.php as I understand they require use of CI's form_open() and won't work properly without it. It's my understanding that excluding parameters from $this->input->post() will return ALL the post items yet I continue to get none. I've also tried going around CI's input library and accessing the data via PHP's $_POST variable, it has made no difference.
Your post data is not in query string format so you should skip dealing with $_POST and go straight to the raw post data.
try
var_dump($HTTP_RAW_POST_DATA);
or even better
var_dump(file_get_contents("php://input"));
in codeigniter 2.X, you can override Input class and add necessary functionality.
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
add file MY_Input.php to application/core
add code inside this file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Input extends CI_Input {
public function raw_post() {
return file_get_contents('php://input');
}
public function post($index = NULL, $xss_clean = FALSE) {
$content_type = $this->get_request_header('Content-type');
if (stripos($content_type, 'application/json') !== FALSE
&& ($postdata = $this->raw_post())
&& in_array($postdata[0], array('{', '['))) {
$decoded_postdata = json_decode($postdata, true);
if ((json_last_error() == JSON_ERROR_NONE))
$_POST = $decoded_postdata;
}
return parent::post($index, $xss_clean);
}
}
that's it..
use it like normal post..