OKSDK.Widgets.post

Method signature

To enable this widget your application requires to have an approve. To get an approve please send a message to api-support@ok.ru email

This SDK is deprecated
For games and applications launched from OK please consider using multiplatform SDK

Publish a topic to user’s feed via publishing widget.

OKSDK.Widgets.post(returnUrl, options)

Where:

  • returnUrl - URL on which user will be redirected after a post was confirmed / declined. Optional parameter;
  • options - additional widget parameters, object:
    • attachment - topic content;
    • silent - automatically close widget window on confirm / decline. Is ignored if returnUrl parameter is specified;
    • utext - allow user to add a comment to a topic;
    • nohead - show OK header in a widget window.

Full parameters description and topic blocks list can be found here - publishing widget.

Return URL message

If returnUrl parameter was specified and options.silent was not user will be redirected to a specified URL.

Method’s result will be passed in an URL as additional GET parameters:

  • result - method’s result, object:
    • type - result status:
      • success - topic was successfully created;
      • error - an error occurred or publishing was declined by a user.
    • code - error code. More information about error codes and their meanings can be found here
    • message - error message;
    • id - published topic id. If you application is not approved topic id is going to be 0 and topic won’t be published;
    • signature - request signature.

If topic was successfully created result will be

https://return.url/
   ?result={"type":"success","id":"123456789123456","signature":"5c1a50d97a870c2****9d68cda15756"}

If user cancelled publishing

https://return.url/
   ?result={"type":"error","code":12,"message":"Operation was cancelled by user"}

If an error occurred

https://return.url/
   ?result={"type":"error","code":104,"message":"Bad signature "}

Window postMessage

If returnUrl was not specified an options.silent was then postMessage with method’s result will be sent to a parent window.

Message will contain data field with all the valuable data about method’s result. This data is same as in case of returnUrl specified.

If topic was successfully created result will be

{
    "id": "69704158565649"
}

If user cancelled publishing

{
    "error_code": "12",
    "error_msg": "Operation was cancelled by user",
    "message": "Operation was cancelled by user",
    "error": "12"
}

If an error occurred

{
    "column": "0",
    "error_code": "4",
    "error_msg": "Invalid JSON at line 0 and column 0",
    "message": "Invalid JSON at line 0 and column 0",
    "error": "4",
    "line": "0"
}

You can add an eventListener which will process such messages:

    window.addEventListener('message', function (widgetMessage) {
        console.log(JSON.stringify(widgetMessage.data));
        // result handling code goes here
    }, false);
    

Example usage

With returnUrl specified

OKSDK.Widgets.post(
    "https://some-return-url.com",
    {
        "attachment": {
            "media": [
                {
                    "type": "text",
                    "text": "Some text"
                },
                {
                    "type": "link",
                    "url": "https://mail.ru/"
                }
            ]
        }
    }
)

Example with a postMessage

OKSDK.Widgets.post(
    null,
    {
        "attachment": {
            "media": [
                {
                    "type": "text",
                    "text": "Play and get a bonus!"
                },
                {
                    "type": "app-ref",
                    "appId": 194424064
                }
            ]
        },
        "silent": true
    }
)

Method call results in a widget being opened in a new window with a topic preview. User can either decline or confirm topic publishing:

Publishing widget