Microsoft Graph Create Contact Issue - php

When I create a contact in microsoft graph I'm getting the following issue
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/json
request-id: xxx
client-request-id: xxx
Strict-Transport-Security: max-age=31536000
Date: Fri, 29 May 2020 13:23:55 GMT
Content-Length: 315
{
"error": {
"code": "RequestBodyRead",
"message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected.",
"innerError": {
"request-id": "xxxx",
"date": "2020-05-29T13:23:55"
}
}
}
This is happening with the following postdata
POST https://graph.microsoft.com/v1.0/users/xxx/contacts HTTP/1.1
Host: graph.microsoft.com
Content-Type: application/json
SdkVersion: Graph-php-1.12.0
Authorization: xxxx
User-Agent: GuzzleHttp/6.5.3 curl/7.51.0 PHP/5.6.31
Content-Length: 209
{"title":"Mr.","givenName":"Eric","surname":"Klinitski","businessPhones":"30 1235436","mobilePhone":"00 49 1234 6543125","emailAddresses":[{"address":"erick#xxx.com","name":"erick#xxx.com"}]}
I'm using the PHP SDK I've managed produce get requests already

Related

Can't get JSON request from my local server in Ionic 4

I'm trying to get a JSON from my local php server into a ionic 4 application. First I discovered that I had to use "ionic cordova emulate browser" instead of "ionic serve" command in order to make HTTP work, since cordova is required. After this, I could reach a JSON example from jsonplaceholder website, but I still can't reach my local server. I tried all possible combinations of urls for my local server and I have no clue what to do next. The codes:
tab1.module.ts
import { Component } from '#angular/core';
import { HTTP } from '#ionic-native/http/ngx';
import { Tab1Service } from '../api/tab1/tab1.service'
#Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
//v: requests from my local server
v1: any;
v2: any;
v3: any;
v4: any;
v5: any;
//a: requests from other server
a1: any;
a2: any;
constructor(public http: HTTP, public tab1Service:Tab1Service) {
//My server
//With HTTP -------------
//Set v1
this.http.get('http://192.168.0.21/WebServer/Ionic/process.php', {}, {})
.then(res => {
this.v1 = res.data;
}
)
.catch(err =>
this.v1 = err
);
//Set v2
this.http.get('192.168.0.21/WebServer/Ionic/process.php', {}, {})
.then(res => {
this.v2 = res.data;
}
)
.catch(err =>
this.v2 = err
);
//Set v3
this.http.get('http://localhost/WebServer/Ionic/process.php', {}, {})
.then(res => {
this.v3 = res.data;
}
)
.catch(err =>
this.v3 = err
);
//Set v4
this.http.get('localhost/WebServer/Ionic/process.php', {}, {})
.then(res => {
this.v4 = res.data;
}
)
.catch(err =>
this.v4 = err
);
//With HttpClient ---------------
//Set v5
this.tab1Service.getConfig()
.subscribe((data) => this.v5 = data['dados']);
//Other server
//With HTTP -------------
//Set a1
this.http.get('https://jsonplaceholder.typicode.com/todos/1', {}, {})
.then(res => {
this.a1 = res.data;
}
)
.catch(err =>
this.a1 = err
);
//With HttpClient ---------------
//Set a2
this.tab1Service.getConfig2()
.subscribe((data) => this.a2 = data['title']);
}
}
PS: All the urls combinations work in the browser
tab1.service.ts
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
#Injectable({
providedIn: 'root'
})
export class Tab1Service {
constructor(private http: HttpClient) {}
getConfig() {
return this.http.get('http://192.168.0.21/WebServer/Ionic/process.php');
}
getConfig2() {
return this.http.get('https://jsonplaceholder.typicode.com/todos/1');
}
}
process.php
<?php
$result = "{ \"success\": true, \"dados\": \"teste\" }";
//Echo $result and set headers
echo $result;
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json; charset=utf-8");
PS: I have the same problem with and without these headers
Prints:
Tab One page: I print all the variables in the same order that they appear in tab1.module.ts in Tab One Page (I created the ionic tab project)
JSON output from both servers
Response Headers from both servers
Console error output when setting v5: This error seems to be related to HttpClient function
Versions:
PHP 7 with Apache Lounge
Ionic 4.10.3
Cordova 8.1.2
Everything I did was based on internet tutorials since I'm pretty new to ionic and php.
EDIT 1
The images information as text:
Tab One page output (Vs {{v1}}, {{v2}}, {{v3}}, {{v4}}, {{v5}} | As {{a1}}, {{a2}})
Vs
[object Object],
[object Object],
[object Object],
[object Object], |
As
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed":
false },
delectus aut autem
JSON from both servers
My server
{ "success": true, "dados": "teste" }
jsonplaceholder
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Response Headers from both servers
My server
Access-Control-Allow-Credentials: true
Connection: Keep-Alive
Content-Length: 37
Content-Type: application/json; charset=utf-8
Date: Tue, 19 Mar 2019 14:55:59 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.38 (Win64) PHP/7.3.3
X-Powered-By: PHP/7.3.3
jsonplaceholder
Access-Control-Allow-Credentials: true
Cache-Control: public, max-age=14400
CF-Cache-Status: HIT
CF-RAY: 4ba06d22bfc3ba18-ATL
Connection: keep-alive
Date: Tue, 19 Mar 2019 15:19:43 GMT
Etag: W/"53-hfEnumeNh6YirfjyjaujcOPPT+s"
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-
cgi/beacon/expect-ct"
Expires: Tue, 19 Mar 2019 19:19:43 GMT
Pragma: no-cache
Server: cloudflare
Vary: Origin, Accept-Encoding
Via: 1.1 vegur
X-Content-Type-Options: nosniff
X-Powered-By: Express
Console error
ERROR {…} ​error: error​​bubbles: false​​cancelBubble: false​​cancelable: false​​
composed: (...)​ message: "Http failure response for
http://192.168.0.21/WebServer/Ionic/process.php: 0 Unknown Error"​ name:
"HttpErrorResponse"​ok: false​status: 0​ statusText: "Unknown Error" ​url:
"http://192.168.0.21/WebServer/Ionic/process.php"​
EDIT 2
Guys, I found what was wrong with my request. The Access-Control-Allow-Origin header was missing. So, when I added:
header("Access-Control-Allow-Origin: *");
in the end of process.php, it worked.
EDIT 3
IMPORTANT: I've just learned that if you add this header you actually put your server in risk, since it becomes more vulnerable to CSRF attack and other similar attacks. So, the right thing to do is to add the specific origins that need to access the server and not use * as a value for Access-Control-Allow-Origin header.

Method Not Allowed on API Search

I'm using Vue.js and Laravel in combination with Axios to make a search filter for my records. When I make the request, I get the following error in my console.
GET http://localhost:8000/api/quests/search/?keywords=test 405 (Method
Not Allowed)
Vue.js
export default {
data() {
return {
quests: [],
quest: {
id: '',
name: '',
price: '',
},
keywords: null,
}
},
watch: {
keywords(after, before) {
this.fetchSearch();
}
},
methods : {
fetchSearch() {
axios.get('/api/quests/search', { params: { keywords: this.keywords}}, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(res => console.log(res.data))
// data empty
.catch(error => console.log(error));
}
}
API route
Route::get('quests/search', 'CrudsController#search');
Controller
public function search(Request $request)
{
$quests = Quest::where('name', 'like', $request->keywords)->get();
return QuestResource::collection($quests);
}
Network response
Cache-Control: no-cache, private
Connection: close
Content-Type: application/json
Date: Mon, 03 Dec 2018 16:08:05 +0000
Date: Mon, 03 Dec 2018 16:08:05 GMT
Host: localhost:8000
X-Powered-By: PHP/7.2.10
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
What am I doing wrong here?
You might be missing csrf token.
Try adding it to your header
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
Or you could alternatively exclude this route vie adding it to $exclude array on csrf middleware.

Laravel 5.4 Passport: unsupported_grant_type

Hello I try to do a web application and I have a problem.
When I request the token from /oauth/token I receive this response:
{"error":"unsupported_grant_type","message":"The authorization grant type is not supported by the authorization server.","hint":"Check the `grant_type` parameter"}
And my code is:
import { Injectable } from '#angular/core';
import { Http, Response, RequestOptions, Headers } from '#angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
#Injectable()
export class UserService {
private API: string = 'api/';
private OAuth: string = 'oauth/'
constructor(private _HTTP: Http) { }
private get_header(): RequestOptions {
let headers = new Headers();
headers.append('Access-Control-Allow-Origin', '*' );
headers.append('Content-Type', 'application/x-www-form-urlencoded' );
return new RequestOptions({ headers: headers });
}
Signin(email: string, password: string): void {
let data = {
form_params: {
grant_type: 'password',
client_id: 2,
client_secret: 'vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms',
username: email,
password: password,
scope: '*',
}
};
this._HTTP.post(
this.OAuth + 'token',
data,
this.get_header()
).toPromise()
.then( (_response) => {
console.log (_response);
});
}
}
And the request header:
POST /oauth/token HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8000/signin
Access-Control-Allow-Origin: *
Content-Type: application/json
X-XSRF-TOKEN: eyJpdiI6IkxtOHhqd0RDUW9MVjl1YVh0U0c4N2c9PSIsInZhbHVlIjoiUmdrenlLWll2eEFtdGFQK2dsWGN0Nm5jWGY2MW5HXC9zMDJFdU52SEh4RUoxSkY1QWVHUFNySXFkUUQ3SDNaTW0zNll6SVRONlFHQjBFVzZPT0RxQkR3PT0iLCJtYWMiOiIwNzg5ZjliMzUwYjE5ZWM4MWE3MTg3NDRjYWZiMDE1MWI1NWJjN2E1NmI5ZTMzY2UzMTIwODI4ODY0ZDQ1ZDY5In0=
Content-Length: 208
Cookie: XSRF-TOKEN=eyJpdiI6IkxtOHhqd0RDUW9MVjl1YVh0U0c4N2c9PSIsInZhbHVlIjoiUmdrenlLWll2eEFtdGFQK2dsWGN0Nm5jWGY2MW5HXC9zMDJFdU52SEh4RUoxSkY1QWVHUFNySXFkUUQ3SDNaTW0zNll6SVRONlFHQjBFVzZPT0RxQkR3PT0iLCJtYWMiOiIwNzg5ZjliMzUwYjE5ZWM4MWE3MTg3NDRjYWZiMDE1MWI1NWJjN2E1NmI5ZTMzY2UzMTIwODI4ODY0ZDQ1ZDY5In0%3D; laravel_session=eyJpdiI6ImlrdlNMTGtTK241WVArZGx6MzE5Mnc9PSIsInZhbHVlIjoiRVQxSmlpZFwvV3B4eVRHVUdVYlRtY1VOZHUzZ09FQnMyZjhjSnZoQjA0VVBvM0x5YnJJbmx3b25cL3dCbVZScTVUb2lTVkg5Sldyd3R0aFluMDBvcmhxQT09IiwibWFjIjoiZDk4NjZkMDhiNTE0NzA3YzQxODVkNGJjN2E3OTRjNWEzMjc2Njk2ZjEyODY2MzY3NmRhYzAzN2U1NGE0ZTg4NCJ9
Connection: keep-alive
Response header:
HTTP/1.1 400 Bad Request
Host: localhost:8000
Connection: close
x-powered-by: PHP/7.1.1
Content-Type: application/json
Cache-Control: no-cache, private
Date: Thu, 27 Jul 2017 09:35:53 +0000, Thu, 27 Jul 2017 09:35:53 GMT
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Post data:
{
"form_params": {
"grant_type": "password",
"client_id": 2,
"client_secret": "vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms",
"username": "fasfa",
"password": "fasfa",
"scope": "*"
}
}
I don't have any other detail.
You need to install a password client for Passport. Assuming you've installed and configured Passport, to generate a password client, run:
php artisan passport:client --password
Make sure to use the details from the output of this command in your API requests.
inside form-data in postman use following
grant_type : password
client_id : 2
client_secret : vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms or whatever this is
username: myemail#yahoo.com
password: mypassword
scope:
You can get client id and secret from oauth_clients table if no data there use following command to create a client
php artisan passport:client --password
use the oauth_clients table particular row, where password client column value is 1
see how i have integrated token generation code with login. I used right format to solve grant issue in laravel 5.8
public function login(Request $request) {
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
$user = Auth::user();
if(Auth::check()) { // check whether user exist
$req = Request::create('/oauth/token', 'POST',[
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'WDv6wrY9Tf9HuixaiQjDWGy7yBqEG1IrmsLucAUI',
'username' => request('email'),
'password' => request('password'),
'scope' => ''
]);
$res = app()->handle($req);
$responseBody = json_decode($res->getContent()); // convert to json object
return response()->json(['success' => $responseBody], $res->getStatusCode());
} else {
return response()->json(['error' => 'Not logged in '], 401);
}
} else {
return response()->json(['error' => 'Authentication failed '], 401);
}
}
I used the following request on Fiddler, it worked fine :
check the header Content-Type:
'Content-Type': 'application/json'

POST JSON Object with multiple headers in php

Request header
Content-Type: application/json
Authorization: Bearer [access token]
Accept: application/json
if send unicode string with message, please set Content-Type charset to UTF-8
Following is a sample request of send service.
URL-https://example.com/requests
Method - POST
Body
{
"outboundSMSMessageRequest": {
"address": [
"tel:+94771234567"
],
"senderAddress": "tel:12345678",
"outboundSMSTextMessage": {
"message": "Test Message"
},
"clientCorrelator": "123456",
"receiptRequest": {
"notifyURL": "http://128.199.174.220:1080/sms/report",
"callbackData": "some-data-useful-to-the-requester"
},
"senderName": "ACME Inc."
}
}
How to post and get responce
ajax
headers: {
'X-HTTP-Method-Override': 'POST',
'Content-Type': 'application/json'
},
or vanilla js
How to create javascript POST request to server with Headers and Data

Can't get properties from JavaScript object created from JSON data generated by PHP script

I have a PHP script that makes query to DB and returns the result as JSON data. This file contains some Codeigniter specific functions.
This functions recieve id and returns back to the JS code some data from a table.
public function get_l($id){
//$id is not empty variable
$this->db->where('id',$id);
$q=$this->db->get('news');
$res = $q->result_array();
$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Content-Type: application/json; charset=utf-8");
$this->output->set_header("Pragma: no-cache");
$out = json_encode($res);
$this->output->set_output($out);
}
And then I need to process that JSON with the next JS code:
function getContent(id){
$.post('/admin_ajax/news/get',
{
id: id
},
function(result)
{
alert(result+"\n"+result.length);//this will output [object Object] 1
for (var key in result)
{
alert(key+':'+result[key]); //and this 0:[object Object]
}
alert(result.title); //here we are getting undefined
},
'json'
);
I am not receiving errors or warnings in console. And in the firebug I see what was returned from the server.
HTTP headers:
Server nginx/1.1.19
Date Fri, 26 Oct 2012 11:59:12 GMT
Content-Type application/json; charset=utf-8
Content-Length 85
Connection keep-alive
X-Powered-By PHP/5.3.10-1ubuntu3.4
Cache-Control post-check=0, pre-check=0
Pragma no-cache
And response:
[{"id":"5","title":"test","alias":"test","content":"tes","date":"123","type":"test"}]
JSON:
alias "test"
content "tes"
date "123"
id "5"
title "test"
type "test"
I found a similar question here but it wasn't helpful.
Your JSON object is in an array [...] hence the length 1.
function(result) {
var jsonObj = result[0];
It seems like the response you are getting is actually an array with one element (the json object).
Try using result[0], like so:
....
function(result)
{
alert(result[0]+"\n"+result[0].length);//this will output [object Object] 1
for (var key in result[0])
{
alert(key+':'+result[0][key]); //and this 0:[object Object]
}
alert(result[0].title); //here we are getting undefined
}
....
Result is an array with one element:
[
{
"id": "5",
"title": "test",
"alias": "test",
"content": "tes",
"date": "123",
"type": "test"
}
]
The element at index 0 contains the information you need. You need to do:
alert(result[0].id);
alert(result[0].title);
alert(result[0].alias);

Categories