# Yahoo Dsp API # 產生Access token的流程 必要條件,要先有clientID clientSecret 參考文件 https://developer.yahooinc.com/identity/guide/enhanced-attribution/authentication/ must change scope "upload" as "dsp-api-access" must change realm "aaca" as "dsp" you can see scope and realm line 27 28 ```java= Java package sample.aaca; import com.google.gson.Gson; import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.apache.http.HttpResponse; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; public class JavaSample { private static String oAuthURL = "https://id.b2b.yahooinc.com/identity/oauth2/access_token"; private static String scope = "upload"; private static String realm = "aaca"; private String clientId = "//Insert ClientId here"; private String clientSecret = "//Insert ClientSecret here"; public static final long ACCESS_TOKEN_TTL = 600000; private String generateJsonWebToken() throws UnsupportedEncodingException { final HashMap<String, Object> claims = new HashMap<>(); long nowMillis = System.currentTimeMillis(); long expMillis = nowMillis + ACCESS_TOKEN_TTL; claims.put("iss", clientId); claims.put("sub", clientId); claims.put("aud", oAuthURL + "?realm=" + realm); claims.put("exp", expMillis / 1000); claims.put("iat", nowMillis / 1000); JwtBuilder jwtBuilder = Jwts.builder().setClaims(claims); return jwtBuilder.signWith(SignatureAlgorithm.HS256, clientSecret.getBytes("UTF-8")).compact(); } private Response getTokenFromAuthServer(String assertion) throws IOException { Response response = null; try (CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).build()) { StringBuilder payload = new StringBuilder().append("scope=").append(scope).append("&grant_type=") .append("client_credentials").append("&client_assertion_type=") .append("urn:ietf:params:oauth:client-assertion-type:jwt-bearer").append("&realm=").append(realm) .append("&client_assertion=").append(assertion); HttpPost request = new HttpPost(oAuthURL); StringEntity body = new StringEntity(payload.toString(), ContentType.APPLICATION_FORM_URLENCODED); request.setEntity(body); request.addHeader("Accept", ContentType.APPLICATION_JSON.toString()); System.out.println("Starting token request.........."); HttpResponse result = httpClient.execute(request); System.out.println("Token request completed.......... " + result.getStatusLine().getStatusCode() + " " + result.getStatusLine().getReasonPhrase()); String json = EntityUtils.toString(result.getEntity(), "UTF-8"); Gson gson = new Gson(); response = gson.fromJson(json, Response.class); } return response; } /** * Get token from server. * @return token generated. * @throws Exception Throws exception if connection issues or encryption issues. */ public String getToken() throws Exception { String assertion = generateJsonWebToken(); Response response = getTokenFromAuthServer(assertion); String token = response.getAccessToken(); return token; } /** * Helper class representing the json response from the IDB2B server */ public static class Response { /** * String with token value received from * the IDB2B server */ private String access_token; public Response() {} public Response(String access_token) { this.access_token = access_token; } public void setAccessToken(String access_token) { this.access_token = access_token; } public String getAccessToken() { return access_token; } } public static void main(String[] args) { JavaSample tokenGenerator = new JavaSample(); String assertion; Response response; try { assertion = tokenGenerator.generateJsonWebToken(); response = tokenGenerator.getTokenFromAuthServer(assertion); System.out.println(response.getAccessToken()); } catch (Exception e) { System.out.println("Exception occured..." + e.getMessage()); } } } ``` when you get the access token which print by main ```java= response = tokenGenerator.getTokenFromAuthServer(assertion); System.out.println(response.getAccessToken()); ``` ## Step two Get All AdvertiserIDs https://dspapi.admanagerplus.yahoo.com/traffic/advertisers -Header "X-Auth-Method: OAuth2" \ -Header "X-Auth-Token: accesstoken" the accesstoken is something like this :3f94eb47-a295-4977-a375-e27bea5c828b
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up