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

1

1

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

Reginareginald answered 13/8, 2014 at 5:11 Comment(0)
S
1

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.

Stereoscopy answered 13/8, 2014 at 22:12 Comment(8)
Thank you. I tried inserting the array as you mentioned. (updated code in question). But I still get null response from the account insert method. Not sure how to proceed now.Reginareginald
Can you log the raw body content of the POST request actually being sent to the server so that I can see if anything is missing/wrong?Stereoscopy
Post Request, Response and Codes are here: gist.github.com/Lubna-10p/…. By debugging, I see that 'call' method of Resource.php successfully calls 'auth->sign' method(response code 200). however, for mirror api call, it returns response code 204 (No Content)Reginareginald
Was the response in the Gist that you linked generated from the API Explorer or from client-side Javascript (judging by the response headers)? That's not going to work because you can't use the service account flow through Javascript; I need to see the raw request and response that your PHP application is sending/receiving. It also looks suspect that your request URL contains the service account e-mail address as the username (it should be the name of the user whose account you're adding), but that shouldn't cause this problem. You'll want to fix that too, though.Stereoscopy
Yes. This is from client side js. I am not allowed to log raw data due to server limitation. Is there any other data i can share? Are we talking about $accountName in $service->accounts->insert($userToken, $accountType, $accountName, $postBody);? What exactly should it contain? name of user or email of user? I am not able to find a working example online and it is causing me problems.Reginareginald
I have updated the request objects at gist.github.com/Lubna-10p/… as much as possible.Reginareginald
Thanks. Yes, the $accountName parameter to accounts->insert should be a username for the user whose account you're inserting -- it will be made available on Glass through the AccountManager. Right now, you're passing the service account e-mail into that. One other thing that stands out to me is that you're passing a browser API key into the service account call here: $client->setDeveloperKey($browser_api_key);. Try removing that from the get_service_client() function and see if that changes anything.Stereoscopy
It works now! Seems like Google API is returning NULL parameter but it is inserting the user account as required. Working code can be seen here: goo.gl/DVggO6Reginareginald

© 2022 - 2024 — McMap. All rights reserved.