mapboxstrava-api-v3mapbox-api-staticimages

Static image api is not working in some cases


I am generating static images for polylines which is fetched from strava api. Now the issue is for some polylines the url is working fine but for others is shows the following error:

{
"message": "Not Authorized",
"error_detail": "Direct access not allowed"
}

URL: https://api.mapbox.com/styles/v1/mapbox/streets-v12/static/path-5+f44-0.5(paste_polyline_here)/auto/1000x1000?access_token=paste_access_token_here

uticDwwgmMBI@UF_@BC\I\FNARFFVBx@IbA@h@AFKDi@Dg@@KAEEGeAFsAHs@^KL?d@FRNDF@@Eb@@@Ct@EPWJG?MBi@BK?ECEOAiAHuAB]DId@EVBN?F@JJBF@d@?p@ElACJSJWDi@@ICMWAa@F_CF[l@Ef@DPTHl@Gr@CfA]

uticDwwgmMBI@UF_@BC\I\FNARFFVBx@IbA@h@AFKDi@Dg@@KAEEGeAFsAHs@^KL?d@FRNDF@@Eb@@@Ct@EPWJG?MBi@BK?ECEOAiAHuAB]DId@EVBN?F@JJBF@d@?p@ElACJSJWDi@@ICMWAa@F_CF[l@Ef@DPTHl@Gr@CfA]Rw@FMCKWEW?oAFcAFUFAZ?LELDX?LP@HBl@A\CJ@XCNA@CLUHu@DM?KCEYCkANsBBAT@A?HCr@BFLDBDJBX?VIpBCFWJ_AFIAGQASGy@PwBj@ITDNAX\@FABEV@^AHWNcAFGACCG[Eq@B[AUJ{ADALB`A@DBBNJ@FL?NIrA@DEVBN?ZIDGCq@F[AGGMq@Ai@LsBDCZCp@BHFDNB?B@@TAz@@VCR?f@AHKH[BABs@OII


Solution

  • The problem is ? (question mark) in the polyline which completely changes the url and hence the authentication token is not recognised so that the error is coming.

    So what we need to do is to encode the polyline and then attach it with the complete url. Here is the code sample I am sharing:

    import java.net.URLEncoder
    val encodedPolyline = URLEncoder.encode(polyline)