文档

Development

User Acquisition

Monetization

工业

Unity Ads Monetization

SDK 4.20.0

受支持
​

Android API reference

Unity Ads Monetization

SDK 4.20.0

受支持
​

此页面不支持所选语言。
Android developer guides
​
​
Android developer guides
  • Android SDK integration guide
  • Unity Ads privacy guide
  • Unity Ads SDK troubleshooting guide
  • Android developer API references
  1. Unity Ads SDK Android developer monetization
  2. Unity Ads SDK integration guide for Android developers

Server-to-server redeem callbacks

Set up server-to-server callbacks to securely validate rewarded ad completions to help prevent fraud and ensure accurate reward distribution.
阅读时间1 分钟
最后更新于 1 个月前

Use Server-to-server (S2S) callbacks to detect and prevent cheating when rewarding players.

How it works

When a player views a video ad in its entirety, the Unity Ads server sends a signed callback to the URL you've specified. This occurs before the actual end of the video, so the game can complete the reward cycle before the player returns to gameplay.
A diagram of the S2S redeem callback process.
提示
Depending on the traffic, callbacks can take some time to arrive. To ensure a smooth gameplay experience, reward players immediately, then use S2S callbacks for sanity checks against cheating. Show the reward notification after the ad view finishes, to avoid distracting the player until the video ends.

Implementation

To use S2S callbacks, you need to set a Server ID (sid) before showing an ad. S2S redeem callbacks are not available by default. If you want to enable them for your project, contact Unity Ads Support with your Game ID(s) and their respective callback URL(s) in the message. Unity will send you a secret hash used to sign and validate the callbacks.

Examples

To implement a callback in your C# code, set the
ShowOptions.gamerSid
value to your Server ID, then pass the options object through the
Show
method.
using UnityEngine;using System.Collections;using UnityEngine.Advertisements;public class UnityAdsManager : MonoBehaviour{ public string gameId; public string placement = "rewardedVideo" public void ShowAd() { ShowOptions options = new ShowOptions(); // setting the server ID options.gamerSid = "your-side-id"; Advertisement.Show(placementID, options); }}
To implement a callback in your Java code, set the
PlayerMetaData.setServerId
value to your Server ID.
PlayerMetaData playerMetaData = new PlayerMetaData(context);playerMetaData.setServerId("example");playerMetaData.commit();UnityAds.show(activity);
To implement a callback in your Objective-C code, set the
playerMetaData.setServerId
value to your Server ID.
UADSShowConfigurationBuilder *builder = [[UADSShowConfigurationBuilder alloc] init];builder = [builder withCustomRewardString:@"your-server-id"];[self.rewardedAd show:[builder build] delegate:delegate];
To implement a callback in your Objective-C code, set the
playerMetaData.setServerId
value to your Server ID.
let config = UADSShowConfigurationBuilder().with(customRewardString: "your-server-id").build()rewardedAd?.show(config, delegate: delegate)

Callback information

Callback origin

The callback will come from the IP addresses/networks listed in here. The list will be updated on the first of each month. Publishers can safely ignore or block callbacks from anywhere else.

Callback URL format

The request is an HTTP/1.1
GET
request to a URL of the following format:
[CALLBACK_URL][SEPARATOR1]sid=[SID][SEPARATOR]oid=[OID][SEPARATOR]hmac=[SIGNATURE]
Refer to the following table for information on query parameters:

Parameter

Content

CALLBACK_URL
The base URL of the callback URL, for example:
https://developer.example.com/award.php?productid=1234
.
To configure this, contact Unity Ads Support
SEPARATOR1
If
?
does not exist in the URL yet, use
?
. Otherwise use
&
is used.
SID
The user ID or any custom data you want to send to your endpoint.
SEPARATOR
&
OID
The unique Offer ID generated by Unity Ads servers.
SEPARATOR
&
SIGNATURE
A HMAC-MD5 hash of a parameter string For example:
106ed4300f91145aff6378a355fced73
.
http title="Example callback URL"https://developer.example.com/award.php?productid=1234&sid=1234567890&oid=0987654321&hmac=106ed4300f91145aff6378a355fced73

Sign the Callback URL

The Callback URL request will have a signature attached to the URL parameters. The signature is a HMAC-MD5 hash of a parameter string created by concatenating all the URL parameters in key-value form (except the HMAC) in alphabetical order, separated by commas.
For example, a callback URL with a SID and OID of
https://developer.example.com/award.php?productid=1234&sid=1234567890&oid=0987654321
will have the following parameter string:
oid=0987654321,productid=1234,sid=1234567890
This, hashed with the secret key you'll receive from Support, returns a hash that the award callback will fire to a URL. For example:
https://developer.example.com/award.php?productid=1234&sid=1234567890&oid=0987654321&hmac=106ed4300f91145aff6378a355fced73
重要
All the parameters included in the callback URL must be included in the signature calculation in alphabetical order. Otherwise the signatures will not match.

Callback response

If the request passes all the checks and user was awarded the item, the URL must reply with a
HTTP/1.1 200 OK
response and include character
1
in the body of the HTTP request. For example:
http title="Callback response example"HTTP/1.1 200 OKDate: Wed, 22 Feb 2012 23:59:59 GMTContent-Length: 81
If there's an error (for example, when the OID has already been used, or signature does not match, or any other error where the user does not end up with the promised item), the server should return an HTTP error in the
400
- or the
500
-range with a human readable error. For example:
http title="Callback error response example"HTTP/1.1 400 ERRORDate: Wed, 22 Feb 2012 23:59:59 GMTContent-Length: 12Duplicate order

Callbacks in node.js

The following example shows how to verify the signature using node.js + express:
js title="node.js callback example"// NODE.js S2S callback endpoint sample implementation// Unity Adsvar express = require("express");var crypto = require("crypto");var app = express();app.listen(process.env.PORT || 3412);function getHMAC(parameters, secret) { var sortedParameterString = sortParams(parameters); return crypto .createHmac("md5", secret) .update(sortedParameterString) .digest("hex");}function sortParams(parameters) { var params = parameters || {}; return Object.keys(params) .filter((key) => key !== "hmac") .sort() .map((key) => (params[key] === null ? `${key}=` : `${key}=${params[key]}`)) .join(",");}app.get("/", function (req, res) { var sid = req.query.sid; var oid = req.query.oid; var hmac = req.query.hmac; // Save the secret as an environment variable. If none is set, default to xyzKEY var secret = process.env.UNITYADSSECRET || "xyzKEY"; var newHmac = getHMAC(req.query, secret); if (hmac === newHmac) { // Signatures match // Check for duplicate oid here (player already received reward) and return 403 if it exists // If there's no duplicate - give virtual goods to player. Return 500 if it fails. // Save the oid for duplicate checking. Return 500 if it fails. // Callback passed, return 200 and include '1' in the message body res.status(200).send("1"); } else { // no match res.sendStatus(403); }});

Callbacks in PHP

The following example shows how to verify the signature in PHP:
php title="PHP callback example"<?phpfunction generate_hash($params, $secret) { ksort($params); // All parameters are always checked in alphabetical order $s = ''; foreach ($params as $key => $value) { $s .= "$key=$value,"; } $s = substr($s, 0, -1); $hash = hash_hmac('md5', $s, $secret); return $hash;}$hash = $_GET['hmac'];unset($_GET['hmac']);$signature = generate_hash($_GET, 'xyzKEY'); // insert here the secret hash key you received from Unity Ads supporterror_log("req hmac".$hash);error_log("sig hmac".$signature);// check signatureif($hash != $signature) { header('HTTP/1.1 403 Forbidden'); echo "Signature did not match"; exit; }// check duplicate ordersif(check_duplicate_orders($_GET['oid'])) { header('HTTP/1.1 403 Forbidden'); echo "Duplicate order"; exit; }// if not then give the player the item and check that it succeeds.if(!give_item_to_player($_GET['sid'], $_GET['product'])) { header('HTTP/1.1 500 Internal Server Error'); echo "Failed to give item to the player"; exit; }// save the order ID for duplicate checkingif(save_order_number($_GET['oid']) { header('HTTP/1.1 500 Internal Server Error'); echo "Order ID saving failed, user granted item"; exit; }// everything OK, return "1"header('HTTP/1.1 200 OK');echo "1";?>

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • How it works

    • Implementation

      • Examples

    • Callback information

      • Callback origin

      • Callback URL format

      • Sign the Callback URL

      • Callback response

        • Callbacks in node.js

    • Callbacks in PHP


报告此页面的问题
​
​