Invalid domain error in gmail setting api - google-api

I want to create a label in gmail account using goggle admin sdk api.
I am sending a Http Post Request to
https://apps-apis.google.com/a/feeds/emailsettings/2.0/{domain}/{username}/label/
But i get this response :-
Invalid domain.
Error 403
Following is the code for this task :-
$domain = explode('#',$user->email);
$headers = array(
"X-HTTP-Method-Override: POST",
"Authorization: {$obj->token_type} "." {$obj->access_token}",
"Content-type: application/atom+xml",
);
$options = array(CURLOPT_URL => "https://apps-apis.google.com/a/feeds/emailsettings/2.0/$domain[1]/$domain[0]/label",
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
// CURLOPT_USERAGENT => '',
CURLOPT_FOLLOWLOCATION => 1,
// CURLOPT_VERBOSE => 1,
// CURLOPT_POST => true
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo "<pre>";print_r($result);echo"</pre>";

If you actually trying to access labels on your gmail.com account from your domain's admin account, it will not work. The email setting API works only on Premier and Edu Google Apps domains administrators.

Related

Opayo merchant session key. Authentication values are missing

We are getting the following error when executing this:
[description] => Authentication values are missing [code] => 1001
Can anyone see what I am doing wrong.
I tried removing the base64_encode function but this didn't have any impact.
I got the integration key and password from opayo (sagepay) - the test environment.
My php code follows:
$key = base64_encode("My Integration Key");
$password = "My Integration Password";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/merchant-session-keys",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{ "vendorName": "MY VENDOR" }',
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $key:$password",
"Cache-Control: no-cache",
"Content-Type: application/json"
)
));
$response = curl_exec($curl);
$response = json_decode($response, true);
$err = curl_error($curl);
curl_close($curl);
Tia for any help
Both the key and password had to be base 64 encoded.
ie $key = base64_encode("My Integration Key:My Integration Password");
and then fed in to the curl http header as
"Authorization: Basic $key",
I had the same issue, so created a NuGet package for this API-client code: https://github.com/joeratzer/opayo-admin-and-reporting-api-client.
You can find the NuGet package here:
https://www.nuget.org/packages/Joe.Opayo.Admin.Api.Client/1.1.0
The client app allows code like this:
var client = new OpayoAdminApiClient();
var isTest = true;
var request = new OpayoApiRequest(ApiCommandType.GetTransactionDetail, "password", isTest, "user-name", "vendor-name");
var commandSpecificXml = "<vendortxcode>01Jan2010Transaction12345</vendortxcode>";
return await client.ProcessApiCommandAsync<TransactionDetail>(request, commandSpecificXml);

Restful API with Laravel

I'm developing a restful API with laravel. Authorization is done using a SECRET_KEY & PUBLIC_KEY as shown in the sample request code below.
How do i retrieve the Authorization header key value from the Request?
I would also appreciate links to any resources that would guide me through better implementation & best practice in creating a Restful API with Laravel.
$curl = curl_init();
$url = "http://api.example.com/v1/users";
$secretKey = env('SECRET_KEY');
$publicKey = env('PUBLIC_KEY');
$firstName = "John";
$lastName = "Doe";
$email = "example#gmail.com";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"firstName\": \"$firstName\",\r\n \"lastName\": \"$lastName\",\r\n \"email\": \"$email\",\r\n \"secretKey\": \"$secretKey\",\r\n}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
'Authorization: Bearer ' . $publicKey,
),
));
$response = curl_exec($curl);
curl_close($curl);
The request() helper contains everything from the request.
Just dd(request()->header()) to see the full list of the request headers.
You can check the official documentaion : https://laravel.com/docs/7.x/eloquent-resources#introduction
Also you might want to use a tool like Postman to test your API easily.

issues Retrieving users profile picture from microsoft outlook account via Microsoft Graph 365 API in PHP

Am trying to get microsoft outlook mail users profile photos using microsoft 365 graph API.
I have set the following permissions User.Read, User.ReadWrite, User.ReadBasic.All, User.Read.All, User.ReadWrite.All on the apps ends but its not displaying any profile photos whereas the user has Profile Photos already uploaded on her outlook mail account....
Below is the code
<?php
session_start();
echo $acc= $_SESSION['access_token'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photo",
//CURLOPT_URL => "https://graph.microsoft.com/v1.0/me",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $acc"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
print_r($response);
$json = json_decode($response);
$res = $json->{'items'};
if ($err) {
echo "cURL Error #:" . $err;
} else {
//echo $response;
}
?>
You're almost there. What you want to use is
https://graph.microsoft.com/v1.0/me/photo/$value

Getting Google YouTube API Service Account Access Token in PHP

I'm trying to obtain an access token from Google so I can use a 'service account' to upload videos to YouTube automatically.
This code:
$credentials = array(
'client_id' => $my_client_id
);
$jwt = JWT::encode($credentials, $private_key);
$client = new Google_Client();
if ($client->authenticate($jwt))
{
// do something
}
Fails with this exception:
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_request: Client must specify either client_id or client_assertion, not both'' in /home/google/client/google-api-php-client/src/Google/Auth/OAuth2.php:120
Where am I going wrong?
TY!
I had missed a large section of the documentation as shown here:
https://developers.google.com/accounts/docs/OAuth2ServiceAccount#creatingjwt
I had also missed that the algorithm had to be RSA256 not HSA256 as defaulted to in the JWT PHP encode function.
And further, that I needed to POST a request, appropriately, directly to get an access token to endpoint:
https://www.googleapis.com/oauth2/v3/token
The Google private JSON private key for the service account also was invalid for use by openssl due to the final character being encoded/included as:
\u003d
Swapping this, literally, with:
=
solved that problem.
Here is my now working (ish, see closing statement) code:
$claimset = array(
'iss' => $client_email,
'scope' => 'https://www.googleapis.com/auth/youtube.upload',
'aud' => 'https://www.googleapis.com/oauth2/v3/token',
'exp' => time() + 1800,
'iat' => time(),
'sub' => 'my google account email#gmail.com'); // not sure if reqd
$jwt = JWT::encode($claimset, $private_key, 'RS256');
// Now need to get a token by posting the above to:
// https://www.googleapis.com/oauth2/v3/token
# Our new data
$data = array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
);
# Create a connection
$url = 'https://www.googleapis.com/oauth2/v3/token';
$ch = curl_init($url);
# Form data string
$postString = http_build_query($data, '', '&');
# Setting our options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Get the response
$response = curl_exec($ch);
curl_close($ch);
print "and here is what we got: ";
print_r($response);
exit;
Unfortunately, for some reason the response I'm getting is:
{ "error": "unauthorized_client", "error_description": "Unauthorized client or scope in request." }
Suspect my service account does not yet have the right to upload to YouTube.
I'm not an experr, but you should probably look at removing the "," at the end of you line here:
'client_id' => $private_key['client_id'],
//'client_email' => $private_ket['client_email']
change to:
'client_id' => $private_key['client_id']
//'client_email' => $private_ket['client_email']
I've used this example to get oauth working. It may be helpful:
http://msdn.microsoft.com/en-us/library/dn632721.aspx
you could also try oauth testing here:
https://developers.google.com/oauthplayground/
Good luck!

Create product using Oauth & curl

Im trying to create product using RESTful Api. Achieved this functionality using RESTCLIENT firefox addon but failed using script. I can able to list products But im not able to create product using script. Getting access denied error. Can anyone help me?
Here is my script.
$url = 'http://magento.com/api/rest/products';
$method = 'POST';
# headers and data (this is API dependent, some uses XML)
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'oauth_signature_method : HMAC-SHA1',
'oauth_nonce : ilJuravy9KVYm6R',
'oauth_timestamp : 1363848967',
'oauth_consumer_key : xxx',
'oauth_consumer_secret : yyy',
'oauth_token : zzz',
'oauth_token_secret : xyz',
'oauth_signature : 4admodOkAj2pKwhO5Tk6TEjc7Rg%3D',
'oauth_verifier: mrr1350pp0j8hiyv31kzxhko97hyyuwx',
'oauth_version : 1.0',
);
$data = json_encode(
array(
'type_id' => 'simple',
'attribute_set_id' => 4,
'sku' => 'simple' . uniqid(),
'weight' => 1,
'status' => 1,
'visibility' => 4,
'name' => 'Simple Product',
'description' => 'Simple Description',
'short_description' => 'Simple Short Description',
'price' => 99.95,
'tax_class_id' => 0,
)
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method) {
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
echo $response = curl_exec($handle);
echo $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
you have to generate 3 things mention below and other things are static like oauth_consumer_key,oauth_token etc.
1.timestmap
2.signature
3.nonce
i have generated all things see below code.
$nonce = substr(md5(uniqid('nonce_', true)),0,16);
$temprealm="http://magentohost/api/rest/products";
$realm=urlencode($temprealm);
$oauth_version="1.0";
$oauth_signature_method="HMAC-SHA1";
$oauth_consumer_key="lro2hnoh3c8luvhcr49j6qgygmyvw7e3";
$oauth_access_token="xbqe4wnu3zv357gimpdnuejvcbtk51ni";
$oauth_method="GET";
$oauth_timestamp=time();
$algo="sha1";
$key="sb88hfdihyg25ipt1by559yzbj2m3861&s7uhaheu8nrx961oxg6uc3os4zgyc2tm"; //consumer secret & token secret //Both are used in generate signature
$data="oauth_consumer_key=".$oauth_consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$oauth_signature_method."&oauth_timestamp=".$oauth_timestamp."&oauth_token=".$oauth_access_token."&oauth_version=".$oauth_version;
$send_data=$oauth_method."&".$realm."&".urlencode($data);
$sign=hash_hmac($algo,$send_data,$key,1); // consumer key and token secrat used here
$fin_sign=base64_encode($sign);
$curl = curl_init();
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization : OAuth realm='.$realm.', oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_nonce="'.$nonce.'", oauth_timestamp="'.$oauth_timestamp.'", oauth_consumer_key='.$oauth_consumer_key.', oauth_token='.$oauth_access_token.', oauth_signature="'.$fin_sign.'"'));
curl_setopt ($curl, CURLOPT_URL,$temprealm);
$xml=curl_exec($curl);

Resources