MapKit JS - Snapshots

I am trying to add an annotation to a snapshot. Without the "&annotations=[]" json bit in the url/signature, it works fine. But the moment I try to add an annotation I get 401 errors.

I am using "C#" to generate the URL and signature. What exactly am I doing incorrectly?

Code Block language
public void GenerateSignature()
{
PrivateKey = System.IO.File.ReadAllText(@"C:\mapkit.p8");
TeamId = config.GetSection("MapKitJS").GetSection("TeamId").Value;
KeyId = config.GetSection("MapKitJS").GetSection("KeyId").Value;
string latLng = "37.78,-122.42";
Annotation = "[{'point':'center','color':'red'}]";
string parameters = string.Format("center={0}&colorScheme=dark&poi=0&z=16&annotations={1}", latLng, Annotation);
string snapshotPath = string.Format("/api/v1/snapshot?{0}", parameters);
string completePath = string.Format("{0}&teamId={1}&keyId={2}", snapshotPath, TeamId, KeyId);
var unsigned = Encoding.UTF8.GetBytes(completePath);
var key = CngKey.Import(Convert.FromBase64String(PrivateKey), CngKeyBlobFormat.Pkcs8PrivateBlob);
using (ECDsaCng dsa = new ECDsaCng(key))
{
dsa.HashAlgorithm = CngAlgorithm.Sha256;
var temp = dsa.SignData(unsigned);
Signature = WebEncoders.Base64UrlEncode(temp);
}
SnapshotUrl = string.Format("https://snapshot.apple-mapkit.com{0}&signature={1}",completePath, Signature);
}


Answered by Frameworks Engineer in 643431022
Hello,

From what I can see in your code block, it looks like you are base64 url encoding the entire signed snapshot url on line 21. However, you might want to base64 url encode only each query parameter value in the URL separately. For example in JavaScript, it would look like:

Code Block ts
let annotation = {
"point": "center",
"color": "red"
};
let snapshotPath = `center=${encodeURIComponent("37.78, -122.42")}&annotations=${encodeURIComponent(annotation)}`;

And then finally add your teamId, keyID and signature query params. Do not base64 url encode after adding the signature query param.
Accepted Answer
Hello,

From what I can see in your code block, it looks like you are base64 url encoding the entire signed snapshot url on line 21. However, you might want to base64 url encode only each query parameter value in the URL separately. For example in JavaScript, it would look like:

Code Block ts
let annotation = {
"point": "center",
"color": "red"
};
let snapshotPath = `center=${encodeURIComponent("37.78, -122.42")}&annotations=${encodeURIComponent(annotation)}`;

And then finally add your teamId, keyID and signature query params. Do not base64 url encode after adding the signature query param.
Well, the "Signature" is what gets base64url encoded. The actual pathing doesn't when it gets returned to the SnapshotUrl. Will try URI encoding the annotation and playing around with it. Will update this post if it works.
URI encoding the Annotation itself did the trick.
MapKit JS - Snapshots
 
 
Q