remove alias using google php api - alias

Is it possible to remove a user's alias using a service account and the google php api?
I have tried several methods, including:
$optParams = array('alias'=>$userMail);
$remove_alias = $directory_service->users->delete($user_info['primaryEmail'],$optParams);
$remove1 = $directory_service->getAuth()->authenticatedRequest($remove_alias);
AND
$url = "https://www.googleapis.com/admin/directory/v1/users/" . $user_info['primaryEmail'] . "/aliases/" . $userMail;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$request_headers = array();
$request_headers['Content-type'] = 'application/json';
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($output, 0, $header_size);
$body = substr($output, $header_size);
curl_close($ch);
I get either error 401, Login Required. Or error, unidentified 'alias'

It looks like calling users->delete vs users_aliases->delete might be causing the issue. Maybe something like this would work?
require_once $CLIENT_API_PATH . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
// this is the service account json file used to make api calls within the domain
$serviceAccount = $JSON_PATH . '/service-account-creds.json';
// delegate the work to an account with ability to make changes
$impersonateUser = 'adminWithRolesToMakeChanges#your.domain.com';
// these are the scope(s) used.
define('SCOPES', implode(' ', array( Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS ) ));
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $serviceAccount );
$client = new Google_Client();
// loads the json service account file.
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$client->setSubject($impersonateUser);
$dirObj = new Google_Service_Directory($client);
// This is the user account to delete the alias from
$userKey = 'joeschmo#your.domain.com';
$alias = 'alias2delete#your.domain.com';
$aliasObj = new Google_Service_Directory_Alias( array( 'alias' => $alias ) );
// delete the alias from the account.
$results = $dirObj->users_aliases->delete($userKey, $aliasObj);
// If successful, this method returns an empty response body.
print_r($results);
See Also:
https://developers.google.com/admin-sdk/directory/v1/reference/users/aliases/delete

Related

Shopify API getting order by name or order_number

Im using a plugin for CakePHP to make the calls to obtain certain orders. I can call all orders with certain fields, but I was wondering how would I have to make the call to get the orders with a certain name or order_number? Here is the source for the call to Shopify. Its already authenticated and everything:
public function call($method, $path, $params=array())
{
if (!$this->isAuthorized())
return;
$password = $this->is_private_app ? $this->secret : md5($this->secret.$this->ShopifyAuth->token);
$baseurl = "https://{$this->api_key}:$password#{$this->ShopifyAuth->shop_domain}/";
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
$request_headers[] = 'X-Shopify-Access-Token: ' . $this->ShopifyAuth->token;
list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);
$this->last_response_headers = $response_headers;
$response = json_decode($response_body, true);
if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
private function shopApiCallLimitParam($index)
{
if ($this->last_response_headers == null)
{
return 0;
}
$params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}
...and the code that makes the GET call:
// I only want the id and title of the collections
$fields = "fields=name,id,status,financial_status,fulfillment_status,billing_address,customer";
// get list of collections
$custom_collections = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
$this->set('collections', $custom_collections);
I think I'm missing the place where I can put the conditions for the call to get certain orders. I've already read the API documentation but can't seem to get the answer.
I've tried putting the ?name=%231001 on the url after .json to try and get the order #1001, but it brings back a empty array.
Then I tried ?order_number=1001 but it brings me every order with as well 1001 D: This is really confusing, Could anyone help me?
Thanks in advance.
Well I found out that you can actually get the order using the name or order_number. Its another property that is not listed on the documentation for some reason. But in the URL, if your using another language, all you have to add in the GET is admin/order.json?name=%2310001&status=any this is to get the order 10001 so just add the order_number after the %23. I saw this on a forum in Shopify university, I was just implementing this wrong on my code. If your using the CakePhp shopify plugin like me all I did was add on the $field the ?name=%23". number ."&status=any";
Ill leave the code here:
$this->layout = 'main';
$order_number = "18253";
$fields = "name=%23". $order_number ."&status=any";
$order = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
if (!empty($order)) {
$this->set('order', $order);
} else {
$this->Session->setFlash('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> No existe el numero de orden ingresado.','default',array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert'));
}
Hope this helps someone :P

PHP Magento API catalog_product.info not working when running through a list

I am trying to create a magento API to get the pricing of each item. I have a table with all the SKU's i need to get info for. i ran the following for one item and it worked
$client = new SoapClient('http://www.mysite.com/api/soap/?wsdl');
$session = $client->login('user', 'pass');
$productId = 'ABC';
$att = array("visibility","sku","special_price", "price");
$arguments = array( $productId, NULL, $att);
$result = $client->call($session, 'catalog_product.info', $arguments);
echo $result['visibility'].",".$result['sku'].",".$result['special_price'].",".$result['price'];
the above code worked fine.
then i tested another code to make sure that my code to query the database and loop through each sku works
$getskus = "SELECT sku FROM items;";
$skus = mysqli_query($con, $getskus);
while($row = mysqli_fetch_array($skus))
{
$productId = $row['sku'];
echo $productId."<br>";
}
The above code works fine. My issue is when i combine the 2 i get a blank screen.
$client = new SoapClient('http://www.mysite.com/api/soap/?wsdl');
$session = $client->login('user', 'pass');
$getskus = "SELECT sku FROM items;";
$skus = mysqli_query($con, $getskus);
while($row = mysqli_fetch_array($skus))
{
$productId = $row['sku'];
$att = array("visibility","sku","special_price", "price");
$arguments = array( $productId, NULL, $att);
$result = $client->call($session, 'catalog_product.info', $arguments);
echo $result['visibility'].",".$result['sku'].",".$result['special_price'].",".$result['price'];
}
i get nothing. Any ideas?
update: if $row['sku'] = '9005' will magento think its a product id instead of a SKU?
This line:
$result = $client->call($session, 'catalog_product.info', $arguments);
This can't accept $arguments as the third param. Instead:
$result = $client->call($session, 'catalog_product.info', $row['sku'], null, $att, 'sku');
NB: not sure if 'null' (4th param) is a valid argument for store view. To be safe, replace with the correct store view (default, in most cases).
RTM: http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.info.html

Cloadflare Return 'rec_id' For 1 'A' Record in a Zone : PHP

I have a PHP script that adds a new 'A' record to a Cloudflare zone, however, by default these new 'A' records are set as non-active by Cloudflare and now days you can not set them as active when creating them.
So, to edit the new record to set it as active, you need the 'A' records 'rec_id'. In this case action 'rec_load_all' can't be used as there are too many zone 'A' records and I don't think you can filter the request (could be wrong & would be good to be wrong). The zone needs to be filtered.
I have tried the following 'dns_get_rec_one' but it just returns 'NULL' with no error message:
function returnId(){
$request = array();
$request['a'] = 'dns_get_rec_one';
$request['tkn'] = $this->tkn;
$request['email'] = $this->apiEmail;
$request['z'] = 'domain.com';
$request['name'] = 'sub.domain.com';
$response = #json_decode(file_get_contents('https://www.cloudflare.com/api_json.html?' . http_build_query($request)), true);
}
Any ideas as I have little experience with API interactions?
Thanks
Ok, I have worked this out with some help.
When you make the CURL 'rec_new' call to Cloudflare the response includes the 'rec_id' for the new "A" record. This can then be used as the 'id' in the next CURL 'rec_edit' call to edit the record as being active.
The guys at Cloudflare support answer within 24hrs as well and are helpful.
Snippets from class bellow:
private function newSub(){
$fields = array(
'a' => 'rec_new',
'tkn' => $this->tkn,
'email' => $this->apiEmail,
'z' => $this->domain,
'type' => 'A',
'name' => $this->subName,
'content' => $this->content,
'ttl' => 1
);
//url-ify the data for the POST
foreach($fields as $key=>$value){
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, 'https://www.cloudflare.com/api_json.html');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$response = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($response,true);
if(!$response || $response['result'] != 'success'){
$responseError = $response['msg'];
// ERROR Handling
}else{
// Set rec_id for from the nw A record
$this->rec_id = $response['response']['rec']['obj']['rec_id'];
// Activate
$this->makeActive();
}
}
private function makeActive(){
$request['a'] = 'rec_edit';
$request['tkn'] = $this->tkn;
$request['email'] = $this->apiEmail;
$request['z'] = $this->domain;
$request['id'] = $this->rec_id;
$request['type'] = 'A';
$request['name'] = $this->subName;
$request['content'] = $this->content;
$request['service_mode'] = '1';// Make active
$request['ttl'] = '1';
$response = #json_decode(file_get_contents('https://www.cloudflare.com/api_json.html?' . http_build_query($request)), true);
//var_dump($response); die;
if(!$response || $response['result'] != 'success'){
$responseError = $response['msg'];
// ERROR Handling
}
}
Hope that this helps someone.

JSON respone from CURL request from HTTP API - HTML transform

I am able to send the CURL request to a test http API and getting the response as well.
My CURL request is like:
$request = "";
$param['auth-userid'] = '449735';
$param['api-key'] = 'apikey';
$param['domain-name'] ='sambalpurodisha';
$param['tlds']='com';
foreach ($param as $key => $val) {$request.= $key ."=".urlencode($val);
$request .="&";}
$request=substr($request,0,strlen($request)- 1 );
$url = "https://test.httpapi.com/api/domains/available.json?".$request;
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec( $ch );
curl_close( $ch );
I am getting a response like:
{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}
Now I have tried everything to format the result in more readable manner like
Domain - sambalpurodisha.com is available for purchase.
I have tried number of suggestions here at stack, but none worked for me. A direction where I can look for
You'll need to decode the returned JSON string as either a JSON object or an associative array, then iterate through it:
$rawJSON = '{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}';
$jsonArray = json_decode($rawJSON, true);
foreach($jsonArray as $key => $val) {
echo 'Domain: ' . $key . ' status: ' . $val['status'] . "\n";
}

Create an video gallery with UStream API

I'm trying to create a simple video gallery that pulls the videos from the recorded shows of a specific Ustream user. I've been doing some research and looking at the API Documentation but can't seem to figure it out.
This is what I have so far.
$request = 'http://api.ustream.tv';
$format = 'php'; // this can be xml, json, html, or php
$args .= 'subject=[channel]';
$args .= '&uid= [the-crossfader-show]';
$args .= '&command=[getCusomEmbedTag]';
$args .= '&params=[autoplay:false; mute:false; height:100; width:100;]';
$args .= '&page=[1]';
$args .= '&limit=[20]';
$args .= '&key=[4872929558631FEB4E9AEE8DDF080F28]';
// Get and config the curl session object
$session = curl_init($request.'/'.$format.'?'.$args);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//execute the request and close
$response = curl_exec($session);
curl_close($session);
// this line works because we requested $format='php' and not some other output format
$resultsArray = unserialize($response);
// this is your data returned; you could do something more useful here than just echo it
print_r $resultsArray['result'];
I'm not really sure what to do after this to turn it into a simple gallery. Anyone have any experience with the UStream API or have suggestions on what to do next?
I know this is an old post, but I have been looking into the API myself for the first time today and came across this. Try using arguments without the square brackets around your values? example:
$args .= 'subject=channel';
$args .= '&uid=the-crossfader-show';
$args .= '&command=getCusomEmbedTag';
$args .= '&params=autoplay:false; mute:false; height:100; width:100;';
$args .= '&page=1';
$args .= '&limit=20';
$args .= '&key=4872929558631FEB4E9AEE8DDF080F28';
You also had an extra space before the-crossfader-show. I haven't tried this code, but perhaps this will help. Also, setting subject=user would get you all the videos for a user, vs subject=channel which i believe to only be a single video stream?