How to insert and retrieve postBody in Mirror API

2019-02-15 22:57发布

问题:

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

回答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.