用PHP创建Evernote商务笔记本

I'm trying to create a new Business Notebook over PHP but I'm having no luck.

I've tried following this documentation and adapting the code to what is available to me using the Thrift module: NoteStore.

From my understanding of how the flow is meant to be, it's; create a new notebook, using the returned notebook get its shareKey, shareID, and businessId, use that information to create a linked notebook in the business note store.

I first tried to creating a new notebook using the non business note store, however the new notebook returned did not contain a shareKey, or any other information needed for creating a linked notebook. Instead I found creating a new notebook using the business note store returned a new notebook with some of this the required information under the 'sharedNotebooks' feild however the businessNotebook parameter was null, and there was no shareId within sharedNotebooks. And even though this new notebook is returned successfull, it is not visable in my personal or business accounts.

The returned notebook looks like

{
    "guid": "d341cd12-9f98-XXX-XXX-XXX",
    "name": "Hello World",
    "updateSequenceNum": 35838,
    "defaultNotebook": false,
    "serviceCreated": 1478570056000,
    "serviceUpdated": 1478570056000,
    "publishing": null,
    "published": null,
    "stack": null,
    "sharedNotebookIds": [603053],
    "sharedNotebooks": [{
        "id": 603053,
        "userId": 40561553,
        "notebookGuid": "d341cd12-9f98-XXX-XXX-XXX",
        "email": "jack@businessEvernoteAccountEmail",
        "notebookModifiable": true,
        "requireLogin": null,
        "serviceCreated": 1478570056000,
        "serviceUpdated": 1478570056000,
        "shareKey": "933ad-xxx",
        "username": "xxx",
        "privilege": 5,
        "allowPreview": null,
        "recipientSettings": {
            "reminderNotifyEmail": null,
            "reminderNotifyInApp": null
        }
    }],
    "businessNotebook": null,
    "contact": {
        "id": 111858676,
        "username": "XXX",
        "email": "jack@personalEvernoteAccountEmail",
        "name": "Jack XXXX",
        "timezone": null,
        "privilege": null,
        "created": null,
        "updated": null,
        "deleted": null,
        "active": true,
        "shardId": null,
        "attributes": null,
        "accounting": null,
        "premiumInfo": null,
        "businessUserInfo": null
    },
    "restrictions": {
        "noReadNotes": null,
        "noCreateNotes": null,
        "noUpdateNotes": null,
        "noExpungeNotes": true,
        "noShareNotes": null,
        "noEmailNotes": true,
        "noSendMessageToRecipients": null,
        "noUpdateNotebook": null,
        "noExpungeNotebook": true,
        "noSetDefaultNotebook": true,
        "noSetNotebookStack": true,
        "noPublishToPublic": true,
        "noPublishToBusinessLibrary": null,
        "noCreateTags": null,
        "noUpdateTags": true,
        "noExpungeTags": true,
        "noSetParentTag": true,
        "noCreateSharedNotebooks": null,
        "updateWhichSharedNotebookRestrictions": null,
        "expungeWhichSharedNotebookRestrictions": null
    }
}

Thus far, my code flow is as follows //Try catches left out for sortness

//Check if business user
$ourUser = $this->userStore->getUser($authToken);
if(!isset($ourUser->accounting->businessId)){
    $returnObject->status = 400;
    $returnObject->message = 'Not a buisness user';
    return $returnObject;
}

// authenticateToBusiness and get business token
$bAuthResult = $this->userStore->authenticateToBusiness($authToken);
$bAuthToken = $bAuthResult->authenticationToken;

//Create client and set up business note store
$client = new \Evernote\AdvancedClient($authToken, false);
$bNoteStore = $client->getBusinessNoteStore();

//Create a new notebook in business note store- example result is json above
$newNotebook = new Notebook();
$newNotebook->name = $title;
$newNotebook = $bNoteStore->createNotebook($bAuthToken, $newNotebook);

//Look at new notebook and get information needed to create linked notebook
$sharedNotebooks = $newNotebook->sharedNotebooks[0];

$newLinkedNotebook = new LinkedNotebook();
$newLinkedNotebook->shareName = $title;
$newLinkedNotebook->shareKey = $sharedNotebooks->shareKey;
$newLinkedNotebook->username = $sharedNotebooks->username;
//$newLinkedNotebook->shardId = $sharedNotebooks->shardId; //isnt there

//This is where I think the trouble is happening ???
$newLinkedNotebook = $bNoteStore->createLinkedNotebook($bAuthToken, $newLinkedNotebook);

//Trying with business token throws 
//{"errorCode":3,"parameter":"authenticationToken"}
//Even though within Evernote itself, I can add notebooks to this business 


//Alternatively trying with the regular $authToken i receive 
//{"errorCode":12,"message":"s570","rateLimitDuration":null}
// error code 12 being SHARD_UNAVAILABLE

//and trying on the regular noteStore 
$newLinkedNotebook = $noteStore->createLinkedNotebook($authToken, $newLinkedNotebook);
//returns {"errorCode":5,"parameter":"LinkedNotebook.shardId"}

OK I've been able to make it work. Basically, there are too mistakes in your code :

  • username and shardId are properties of the user, not of the shared notebook.
  • the linked notebook has to be created on the user store, not the business one. Basically the linked notebook is only a 'link' to the 'real' shared business notebook you created. It allows the user to access the business notebook.

So here some code that work for me :

$client = new \Evernote\AdvancedClient($authToken, false, null, null, false);

$userStore = $client->getUserStore();
$userNoteStore = $client->getNoteStore();

$ourUser = $userStore->getUser($authToken);
if(!isset($ourUser->accounting->businessId)){
    $returnObject = new \stdClass();
    $returnObject->status = 400;
    $returnObject->message = 'Not a buisness user';
    return $returnObject;
}

$bAuthResult = $userStore->authenticateToBusiness($authToken);
$bAuthToken = $bAuthResult->authenticationToken;

$bNoteStore = $client->getBusinessNoteStore();

$title = 'My title';

$newNotebook = new \EDAM\Types\Notebook();
$newNotebook->name = $title;
$newNotebook = $bNoteStore->createNotebook($bAuthToken, $newNotebook);

$sharedNotebook = $newNotebook->sharedNotebooks[0];

$newLinkedNotebook = new \EDAM\Types\LinkedNotebook();
$newLinkedNotebook->shareName = $newNotebook->name;
$newLinkedNotebook->shareKey = $sharedNotebook->shareKey;

$newLinkedNotebook->username = $bAuthResult->user->username;
$newLinkedNotebook->shardId = $bAuthResult->user->shardId;

$newLinkedNotebook = $userNoteStore->createLinkedNotebook($authToken, $newLinkedNotebook);

Hope that helps !

PS: say hi to Chris for me ;)

</div>

I just had a quick look so I may be wrong but the description of the error code n° 12 is : "Service shard with account data is temporarily down"

Might be a temporary error. Let me know and I'll try to take some time to check this issue if it's not the case.