Can't move numbers from subaccount to master account (Twilio) - php

I am using the Twilio PHP helper library.
I have several subaccount credentials stored in the database. Each subaccount usually has several outgoing caller ids associated with it.
I use the credentials for each subaccount to get the outgoingCallerIds.
From the data returned, I take the subaccount SID (AC) and the number SID (PN).
I follow the documentation and am receiving errors on each attempt to update master with the numbers from the subaccounts.
Caught exception: [HTTP 404] Unable to update record: The requested
resource
/2010-04-01/Accounts/ACxxxxxxxxxxxxxx/IncomingPhoneNumbers/PNxxxxxxxx.json
was not found
I have checked the account sids and number sids and they are correct.
Any idea where I'm going wrong here?
Here is my Try that happens in a foreach which credentials for every subaccount.
try
{
$client = new Client($sid, $token); //subaccount sid and token from not shown foreach
$caller_ids = $client->outgoingCallerIds->read();
foreach ($caller_ids as $caller_id)
{
$phone_number = $caller_id->phoneNumber;
$friendly_name = $caller_id->friendlyName;
$number_sid = $caller_id->sid;
$account_sid = $caller_id->accountSid;
$incoming_phone_number = $master_account->incomingPhoneNumbers("$number_sid")
->update(
array("accountSid" => "$account_sid")
);
}
}

Twilio developer evangelist here.
I believe the issue here is that you are using the master account sid to refer to the number that sits within the subaccount thus getting a 404. Instead you should select the subaccount from within the master account's list of accounts, and use that subaccount object to refer to the number as you transfer it.
For example:
$subaccount = $master_account->accounts($subaccount_sid);
$incoming_phone_number = $subaccount->incomingPhoneNumbers($number_sid)
->update(
array("accountSid" => $master_account->sid)
);
Let me know if this helps at all.

Related

Graph User id property returning blank string

I've got a php web app (hosted on Azure) using the microsoft/microsoft-graph SDK for one of my authentication providers.
I am able to get a token and pull some of the user properties, but the 'id' value seems to be returning a blank string:
$me = $provider->get("me", $token);
printf('<br>Hello %s!', $me['displayName']);
printf('<br>First Name: %s', $me['givenName']);
printf('<br>Last Name: %s', $me['surname']);
printf('<br>ID: %s', $me['id']); // returns nothing
printf('<br>Email: %s', $me['userPrincipalName']);
printf('<br>Country: %s', $me['country']);
printf('<br>Postal Code: %s', $me['postalCode']);
According to the User reference, I should be able to get the user ID value as a string.
I am also using thenetworg/oauth2-azure as part of the project and the following does return a GUID. Is it the same ID that I'm looking for? The unique user ID from Graph? Or is it a different ID?
printf('<br>ID: %s', $resourceOwner->getId());
Ideally, I'd like to get the ID value directly from Graph like all the other properties. Is there something I'm missing that I need to do special for the ID property? (well, obviously...) Is the issue with Graph, with the php library, or something else?
Thanks for your assistance.
[Update]
OK, so backing up a step: I've got two pages:
Page 1 has links to a number of authentication options.
Page 2 is a redirect from one of those options - the Microsoft Work and School option.
Page 1 now uses the following to create the link:
$mscallbackUrl = $urlcore . '/ms-callback.php';
$provider = new TheNetworg\OAuth2\Client\Provider\Azure([
'clientId' => $msAppId,
'clentSecret' => $msAppSecret,
'redirectUri' => $mscallbackUrl
]);
$provider->urlAPI = "https://graph.microsoft.com/v1.0/";
$provider->resource = "https://graph.microsoft.com/";
$authUrl = $provider->getAuthorizationUrl();
Page 2 uses the exact same code above to set up $provider then uses the following to connect to Graph:
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
try {
$graph = new \Microsoft\Graph\Graph();
$graph->setAccessToken($token->getToken());
$me = $graph->createRequest("GET", "/me")
->setReturnType(Model\User::class)
->execute();
printf('<br>Hello %s!', $me->getDisplayName());
printf('<br>ID: %s', $me->id);
This code is failing on $me = $graph->createRequest
One reference I found said it could be failing because of an issue with the token.
I think there are a few things that may be causing the confusion. By default, the oauth2-azure library authenticates for the AAD Graph resource (https://graph.windows.net) instead of the Microsoft Graph resource (https://graph.microsoft.com), so you will want to verify that you request an access token for the correct resource.
Secondly, AAD Graph does not return an id field so this will return null. I believe the correlated equivalent field to MS Graph is oid.
Third, you are using the oauth2-azure library to access AAD Graph instead of the microsoft-graph library for Microsoft Graph. Once you get your access token, you can pass that into a new Graph instance like so:
$graph = new \Microsoft\Graph\Graph();
$graph->setAccessToken($token->getToken());
$me = $graph->createRequest("GET", "/me")
->setReturnType(Model\User::class)
->execute();
echo $me->id;

Is it possible to add a ticket response as a specific user via the SoftLayer API using the PHP SOAP client?

I'm attempting to use the SoftLayer API (via the PHP client) to add an update to an existing ticket, as a specific user under our main account (a sibling to our API user).
What I'm trying to achieve is that as the API user, I call SoftLayer_Ticket::addUpdate, passing a SoftLayer_Ticket_Update object that looks similar to:
{
editorID: user1_id_here,
editorType: "USER",
entry: 'My update here'
}
I have user1's ID, however when I pass it in the SoftLayer_Ticket_Update object, the update is still attached as the API user:
{
createDate:"2016-05-17T08:24:50-07:00"
editorId:api_users_id
editorType:"USER"
entry:"Another update for our glorious leader..."
id:#########
ticketId:########
}
Snippet of the ticket update code:
$slClient = \SoftLayer_client::getClient("Ticket", $ticketID);
$ticketUpdateObj = new \stdClass();
$ticketUpdateObj->entry = $update;
$ticketUpdateObj->editorId = ######;
$ticketUpdateObj->editor = ######;
$ticketUpdateObj->editorType = "USER";
$result = $slClient->addUpdate($ticketUpdateObj);
...
(do other stuff here)
Is there any way to do this with the SoftLayer API? Or is the issue stemming from permissions given to the API user, or that I'm attempting to add the update as a sibling to the API user? Any help would be appreciated!
Our platform is older, so we are using an older version of the SoftLayer PHP SOAP client (guessing circa 2010/early 2011).
Thank you in advance!
Sorry, that is not possbile you cannot set the editorId, this value is set by the system with the userID of the user who updated the ticket.
Regards

Amazon SES Error retrieving credentials from the instance profile metadata server. (Client error: 404)

Am having some issues getting AWS SES working per below; I want to send an email to users from my website. Looks like the credentials are not being validated, however I have used the correct credentials generated from an IAM (I also tried the server root keys and it gave me the same error). I have run out of ideas of how to resolved/ debug any further so any steer would be greatly appreciated.
Error Received on Execution:
Error retrieving credentials from the instance profile metadata server. (Client error: 404)
Steps Taken
I have setup SES and validated the email addresses etc
I have created a IAM profile with 'Full access to SES'
I have installed the AWS SDK for php using the phar file
I have written the php code below providing the correct security access code directly of the SES
require 'aws/aws.phar';
use Aws\Ses\SesClient;
//More code here
$client = SesClient::factory(array(
'key' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxx',
'region' => 'us-west-2',
'version' => '2010-12-01'
));
//code to build the $msg here as array
try{
$result = $client->sendEmail($msg);
//save the MessageId which can be used to track the request
$msg_id = $result->get('MessageId');
echo("MessageId: $msg_id");
//view sample output
print_r($result);
} catch (Exception $e) {
echo($e->getMessage());
}
//view the original message passed to the SDK
print_r($msg);
Thank you for your help in advance - this is always a great community!! Please let me know if i can provide anything else
John
You can provide credentials to your SDK using multiple methods. See the documentation: Providing Credentials to SDK
1) Set the environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION with the values from the IAM profile you created.
2) Instead of 1), you can also create ~/.aws/credentials file. Here you can add the lines: [default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
aws_default_region = the region
1) or 2) will definitely work and it is straight forward.
3) You can also create instance profile. You need to create IAM role and instance profile. Your instance needs to have instance profile assigned when it is being created. See page 183 (as indicated on bottom of page. The topic name is "Using an IAM Role to Grant Permissions to Applications
Running on Amazon EC2 Instances") of this guide: AWS IAM User Guide to understand the steps and procedure. Here, the secret key and access key are automatically picked up and you don't have to do anything. You just need to set default region using step 1) (i.e, export AWS_DEFAULT_REGION=someregion).
4) You have already tried the 4th method and may be there is some issue in your settings which I am not aware of.

How to insert and retrieve postBody in Mirror API account insert method using PHP

I need to insert user's email in postBody of mirror API insert method. I am using this code:
$authtoken=null;
$postBody = new Google_Service_Mirror_Account();
$postBody->setAuthTokens($authtoken);
$userdata=array("email"=>$email);
$postBody->setUserData($userdata);
$account = $service->accounts->insert($userToken, package-name-here, $accountName, $postBody);
The above method returns null in response! I am not sure what to add as authtoken.
After this, I need to retrieve user's email account through Android's account manager:
AccountManager manager = AccountManager.get(c);
Account[] list = manager.getAccountsByType(package-name-here);
for (Account acct : list) {
accountEmailId= manager.getUserData(acct, "email");
break;
}
This doesn't seem to work. I do not get accounts of this type in Glass device. Any help will be great.
EDIT:
Added the authTokenArray and userDataArray to postBody as suggested below:
$userDataArray= array();
$userData1= new Google_Service_Mirror_UserData();
$userData1->setKey('email');
$userData1->setValue($email);
$userDataArray[]=$userData1;
$authTokenArray= array();
$authToken1= new Google_Service_Mirror_AuthToken();
$authToken1->setAuthToken('randomtoken');
$authToken1->setType('randomType');
$authTokenArray[]=$authToken1;
$postBody = new Google_Service_Mirror_Account();
$postBody->setUserData($userDataArray);
$postBody->setAuthTokens($authTokenArray);
Account insert method still returns null. Unable to solve the issue till now.
[SOLVED]
Mirror API still returns NULL response, but account is actually being inserted in Mirror API. Updated code can be viewed here: http://goo.gl/DVggO6
setAuthTokens takes an array of Google_Service_Mirror_AuthToken objects (see the source here), each of which has an authToken (an arbitrary string of your choosing) and a type (another arbitrary string). These values are copied directly into the account in the Android AccountManager so that you can look them on the device.
Your problem might be coming from the fact that you're passing in null for that right now. I would try fixing that and then see if you're able to see the account on the device.

How can I log into multiple accounts at the same time using the Google Api PHP Client

Background: We've got two Accounts that each hold several profiles.
I am developing an application in PHP using the provided API.
I can successfully retrieve data from both accounts separately, but whenever I instantiate the Google_Client object a second time (using a different variable name, of course), it instantly logs me out of the first account and overwrites the first account's settings.
Has anyone successfully managed to log into two accounts at the same time using the PHP API Client and could give me a hint on how to accomplish that?
Relevant code sections:
$client1 = new Google_Client();
$client1 ->setAssertionCredentials($omitted);
$client1 ->setClientId($id);
$client1 ->setAccessType('offline_access');
$gaClient1 = new Google_AnalyticsService($client);
//I can now successfully query the Analytics API, but when I do this
$client2 = new Google_Client();
//and query gaClient1 again, it throws a "you must login/401"-error
I guess the problem is that the api uses caching with files to handle request. Now when the second client is created, it is using the same cache folder and thus it is overwriting the previous settings.
now in your config.php is the following:
'ioFileCache_directory' =>
(function_exists('sys_get_temp_dir') ?
sys_get_temp_dir() . '/Google_Client' :
'/tmp/Google_Client')
This is the part that is always returning the same result. When creating a client, you can send in your own config array and it will be combined with the master. So you could use:
$client1 = new Google_Client(array('ioFileCache_directory' => '/tmp/dirclient1'));
$client2 = new Google_Client(array('ioFileCache_directory' => '/tmp/dirclient2'));
Also in your code you created a $client1, but later on you use $gaClient1 = new Google_AnalyticsService($client);. Shouldnt that be $gaClient1 = new Google_AnalyticsService($client1);?

Categories