ทําการเรียก API แรกของคุณ
สิ่งสำคัญ
ในเดือนมิถุนายน ค.ศ. 2022 เราได้แนะนํา การรับรองความถูกต้องแบบหลายปัจจัย เป็นข้อกําหนดสําหรับโฆษณา Bing คุณอาจยังจําเป็นต้องทําการเปลี่ยนแปลงรหัสเพื่อให้สอดคล้องกับข้อกําหนดนี้ Microsoft Advertising จะดําเนินการตรวจสอบการบังคับใช้ทางเทคนิคในช่วงต้นเดือนตุลาคม
โพสต์ในบล็อกนี้จะ สรุปขั้นตอนที่คุณควรดําเนินการเพื่อให้แน่ใจว่าเป็นไปตามข้อกําหนด
สําหรับข้อมูลเพิ่มเติม โปรดดูคําแนะนําข้อกําหนดในการรับรองความถูกต้องแบบหลายปัจจัย
ถ้าคุณเพียงต้องการให้บางสิ่งทํางานทันที ให้ทําตามขั้นตอนเหล่านี้เพื่อรับข้อมูลผู้ใช้ Microsoft Advertising ของคุณ
เริ่มต้นใช้งานด่วนสําหรับการผลิต
เมื่อต้องการรับรองความถูกต้องในสภาพแวดล้อมการผลิต ก่อนอื่นคุณควรลงทะเบียนแอปพลิเคชัน ลงชื่อเข้าใช้ด้วยข้อมูลประจําตัวบัญชี Microsoft ของคุณและให้ความยินยอม ของแอปเพื่อ จัดการบัญชี Microsoft Advertising ของคุณ
- สร้างไฟล์ใหม่ และวางลงในไฟล์สคริปต์ต่อไปนี้ ตั้งค่า
$clientId
เป็น ID แอปพลิเคชันของแอปที่ลงทะเบียนไว้ ถ้าคุณลงทะเบียนแอปพลิเคชันบนเว็บด้วยข้อมูลลับของไคลเอ็นต์ แล้วคุณจะต้องรวม$client_secret=YourWebAppClientSecret
ไว้ด้วยเมื่อร้องขอโทเค็นการเข้าถึง
หมายเหตุ
แทนที่ your_client_id ด้านล่างด้วย ID แอปพลิเคชัน (ไคลเอ็นต์) ที่ พอร์ทัล Azure - พอร์ทัลการลงทะเบียนแอป ที่กําหนดให้กับแอปของคุณ
# Replace your_client_id with your registered application ID.
$clientId = "your_client_id"
Start-Process "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=$clientId&scope=openid%20profile%20https://ads.microsoft.com/msads.manage%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient&state=ClientStateGoesHere&prompt=login"
$code = Read-Host "Grant consent in the browser, and then enter the response URI here:"
$code = $code -match 'code=(.*)\&'
$code = $Matches[1]
# Get the initial access and refresh tokens.
$response = Invoke-WebRequest https://login.microsoftonline.com/common/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=authorization_code&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient"
$oauthTokens = ($response.Content | ConvertFrom-Json)
Write-Output "Access token: " $oauthTokens.access_token
Write-Output "Access token expires in: " $oauthTokens.expires_in
Write-Output "Refresh token: " $oauthTokens.refresh_token
# The access token will expire e.g., after one hour.
# Use the refresh token to get new access and refresh tokens.
$response = Invoke-WebRequest https://login.microsoftonline.com/common/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=refresh_token&refresh_token=$($oauthTokens.refresh_token)"
$oauthTokens = ($response.Content | ConvertFrom-Json)
Write-Output "Access token: " $oauthTokens.access_token
Write-Output "Access token expires in: " $oauthTokens.expires_in
Write-Output "Refresh token: " $oauthTokens.refresh_token
บันทึกไฟล์และตั้งชื่อไฟล์ ( Get-Tokens-Production.ps1
คุณสามารถตั้งชื่อเป็นอะไรก็ได้ที่คุณต้องการ แต่ต้อง .ps1 ส่วนขยาย)
หากต้องการจัดการบัญชี Microsoft Advertising ทางโปรแกรม คุณต้องให้ความยินยอมอย่างน้อยหนึ่งครั้งผ่านโฟลว์ความยินยอมของแอปพลิเคชันบนเว็บ จากนั้นคุณสามารถใช้โทเค็นรีเฟรชล่าสุดเพื่อร้องขอโทเค็นการเข้าถึงและการรีเฟรชใหม่โดยไม่ต้องมีการโต้ตอบกับผู้ใช้เพิ่มเติม
ตอนนี้การเรียกใช้
Get-Tokens-Production.ps1
เปิดหน้าต่างคอนโซล ที่พรอมต์คําสั่ง ให้นําทางไปยังโฟลเดอร์ที่คุณบันทึกGet-Tokens-Production.ps1
และป้อนคําสั่งต่อไปนี้:powershell.exe -File .\Get-Tokens-Production.ps1
เมื่อเรียกใช้สคริปต์ PowerShell สําเร็จ จะมีการเริ่มเซสชันเบราว์เซอร์ที่คุณใส่ข้อมูลประจําตัว Microsoft Advertising ของคุณ หลังจากยินยอม แถบที่อยู่ของเบราว์เซอร์ประกอบด้วยรหัสให้ (ดู ?code=UseThisCode&...)
https://login.microsoftonline.com/common/oauth2/nativeclient?code=M.R4_BAY.f202904c-2269-4daf-1e21-862ed4d49143
คัดลอกรหัสเงินช่วยเหลือ (รหัสของคุณเอง ไม่ใช่ตัวอย่าง M.R4_BAY.f202904c-2269-4daf-1e21-862ed4d49143) และป้อนเข้าไปในหน้าต่างคอนโซลที่พร้อมท์ จากนั้นสคริปต์ PowerShell จะส่งกลับโทเค็นการเข้าถึงและการรีเฟรช (สคริปต์ทําการเรียกครั้งที่สองเพื่อ Invoke-WebRequest เป็นตัวอย่างของวิธีการรีเฟรชโทเค็น) คุณควรถือว่าโทเค็นรีเฟรชเหมือนกับที่คุณต้องการรหัสผ่าน ถ้ามีคนได้รับมันพวกเขามีสิทธิ์เข้าถึงทรัพยากรของคุณ โทเค็นรีเฟรชมีอายุนาน แต่อาจไม่ถูกต้อง ถ้าคุณได้รับข้อผิดพลาด invalid_grant โทเค็นการรีเฟรชของคุณไม่ถูกต้องอีกต่อไป และคุณจะต้องเรียกใช้
Get-Tokens-Production.ps1
สคริปต์ PowerShell อีกครั้งเพื่อรับความยินยอมของผู้ใช้และโทเค็นรีเฟรชใหม่สร้างไฟล์ใหม่ และวางลงในไฟล์สคริปต์ต่อไปนี้
accessToken
ตั้งค่า เป็นค่าที่คุณได้รับGet-Tokens-Production.ps1
และตั้งค่า$developerToken
เป็นโทเค็นนักพัฒนาที่คุณได้รับโดยขั้นตอนต่อไปนี้ที่อธิบายไว้ที่นี่$accessToken = "AccessTokenGoesHere"; $developerToken = "DeveloperTokenGoesHere"; [xml]$getUserRequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13"> <soapenv:Header> <v13:DeveloperToken>{0}</v13:DeveloperToken> <v13:AuthenticationToken>{1}</v13:AuthenticationToken> </soapenv:Header> <soapenv:Body> <v13:GetUserRequest> <v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </v13:GetUserRequest> </soapenv:Body> </soapenv:Envelope>' -f $developerToken, $accessToken $headers = @{"SOAPAction" = "GetUser"} $uri = "https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc" $response = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $getUserRequest -Headers $headers Write-Output $response.Content
บันทึกไฟล์และตั้งชื่อไฟล์ (
Get-User.ps1
คุณสามารถตั้งชื่อเป็นอะไรก็ได้ที่คุณต้องการ แต่ต้อง .ps1 ส่วนขยาย)ตอนนี้การเรียกใช้
Get-User.ps1
เปิดหน้าต่างคอนโซล ที่พรอมต์คําสั่ง ให้นําทางไปยังโฟลเดอร์ที่คุณบันทึกGet-User.ps1
และป้อนคําสั่งต่อไปนี้:powershell.exe -File .\Get-User.ps1
เมื่อสคริปต์ PowerShell ทํางานได้สําเร็จ ควรพิมพ์รายละเอียดของผู้ใช้ Microsoft Advertising รวมถึงบทบาทลูกค้า สําหรับรายละเอียด ดู GetUser
เริ่มต้นใช้งานด่วน Sandbox
เมื่อต้องการรับรองความถูกต้องในสภาพแวดล้อม Sandbox คุณไม่จําเป็นต้องลงทะเบียนแอปพลิเคชัน เพียงแค่ใช้ ID ไคลเอ็นต์ "แอปตัวอย่างบทช่วยสอน" สาธารณะ เช่น 4c0b021c-00c3-4508-838f-d3127e8167ff
ลงทะเบียนสําหรับบัญชี Sandbox โฆษณาของ Microsoft ที่อยู่อีเมลของบัญชี Microsoft (MSA) ต้องเป็น outlook-int.com (ตัวอย่างเช่น someone@outlook-int.com) สําหรับรายละเอียดเพิ่มเติม ดู Sandbox
สร้างไฟล์ใหม่ และวางลงในไฟล์สคริปต์ต่อไปนี้
# Replace the Tutorial Sample App ID with your registered application ID. $clientId = "4c0b021c-00c3-4508-838f-d3127e8167ff" Start-Process "https://login.windows-ppe.net/consumers/oauth2/v2.0/authorize?client_id=$clientId&scope=openid%20profile%20https://api.ads.microsoft.com/msads.manage%20offline_access&response_type=code&redirect_uri=https://login.windows-ppe.net/common/oauth2/nativeclient&state=ClientStateGoesHere&prompt=login" $code = Read-Host "Grant consent in the browser, and then enter the response URI here:" $code = $code -match 'code=(.*)\&' $code = $Matches[1] # Get the initial access and refresh tokens. $response = Invoke-WebRequest https://login.windows-ppe.net/consumers/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=authorization_code&redirect_uri=https://login.windows-ppe.net/common/oauth2/nativeclient" $oauthTokens = ($response.Content | ConvertFrom-Json) Write-Output "Access token: " $oauthTokens.access_token Write-Output "Access token expires in: " $oauthTokens.expires_in Write-Output "Refresh token: " $oauthTokens.refresh_token # The access token will expire e.g., after one hour. # Use the refresh token to get new access and refresh tokens. $response = Invoke-WebRequest https://login.windows-ppe.net/consumers/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://api.ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=refresh_token&refresh_token=$($oauthTokens.refresh_token)" $oauthTokens = ($response.Content | ConvertFrom-Json) Write-Output "Access token: " $oauthTokens.access_token Write-Output "Access token expires in: " $oauthTokens.expires_in Write-Output "Refresh token: " $oauthTokens.refresh_token
บันทึกไฟล์และตั้งชื่อไฟล์ (
Get-Tokens-Sandbox.ps1
คุณสามารถตั้งชื่อเป็นอะไรก็ได้ที่คุณต้องการ แต่ต้อง .ps1 ส่วนขยาย)ผู้ใช้ต้องให้ความยินยอมอย่างน้อยหนึ่งครั้งผ่านโฟลว์ความยินยอมของแอปพลิเคชันบนเว็บ จากนั้นคุณสามารถใช้โทเค็นรีเฟรชล่าสุดเพื่อร้องขอโทเค็นการเข้าถึงและการรีเฟรชใหม่โดยไม่ต้องมีการโต้ตอบกับผู้ใช้เพิ่มเติม
ตอนนี้การเรียกใช้
Get-Tokens-Sandbox.ps1
เปิดหน้าต่างคอนโซล ที่พรอมต์คําสั่ง ให้นําทางไปยังโฟลเดอร์ที่คุณบันทึกGet-Tokens-Sandbox.ps1
และป้อนคําสั่งต่อไปนี้:powershell.exe -File .\Get-Tokens-Sandbox.ps1
เมื่อเรียกใช้สคริปต์ PowerShell สําเร็จ จะมีการเริ่มเซสชันเบราว์เซอร์ที่คุณใส่ข้อมูลประจําตัว Microsoft Advertising ของคุณ หลังจากยินยอม แถบที่อยู่ของเบราว์เซอร์ประกอบด้วยรหัสให้ (ดู ?code=UseThisCode&...)
https://login.windows-ppe.net/common/oauth2/nativeclient?code=M.R0_CD1.132de532-5105-7550-b1fd-d37f9af2f009
คัดลอกรหัสเงินช่วยเหลือ (รหัสของคุณเอง ไม่ใช่ตัวอย่าง M.R0_CD1.132de532-5105-7550-b1fd-d37f9af2f009) และป้อนในหน้าต่างคอนโซลที่พร้อมท์ จากนั้นสคริปต์ PowerShell จะส่งกลับโทเค็นการเข้าถึงและการรีเฟรช (สคริปต์ทําการเรียกครั้งที่สองเพื่อ Invoke-WebRequest เป็นตัวอย่างของวิธีการรีเฟรชโทเค็น) คุณควรถือว่าโทเค็นรีเฟรชเหมือนกับที่คุณต้องการรหัสผ่าน ถ้ามีคนได้รับมันพวกเขามีสิทธิ์เข้าถึงทรัพยากรของคุณ โทเค็นรีเฟรชมีอายุนาน แต่อาจไม่ถูกต้อง ถ้าคุณได้รับข้อผิดพลาด invalid_grant โทเค็นการรีเฟรชของคุณไม่ถูกต้องอีกต่อไป และคุณจะต้องเรียกใช้
Get-Tokens-Sandbox.ps1
สคริปต์ PowerShell อีกครั้งเพื่อรับความยินยอมของผู้ใช้และโทเค็นรีเฟรชใหม่สร้างไฟล์ใหม่ และวางลงในไฟล์สคริปต์ต่อไปนี้
accessToken
ตั้งค่า เป็นค่าที่คุณได้รับจากGet-Tokens-Sandbox.ps1
$accessToken = "AccessTokenGoesHere"; $developerToken = "BBD37VB98"; [xml]$getUserRequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13"> <soapenv:Header> <v13:DeveloperToken>{0}</v13:DeveloperToken> <v13:AuthenticationToken>{1}</v13:AuthenticationToken> </soapenv:Header> <soapenv:Body> <v13:GetUserRequest> <v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </v13:GetUserRequest> </soapenv:Body> </soapenv:Envelope>' -f $developerToken, $accessToken $headers = @{"SOAPAction" = "GetUser"} $uri = "https://clientcenter.api.sandbox.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc" $response = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $getUserRequest -Headers $headers Write-Output $response.Content
บันทึกไฟล์และตั้งชื่อไฟล์ (
Get-User.ps1
คุณสามารถตั้งชื่อเป็นอะไรก็ได้ที่คุณต้องการ แต่ต้อง .ps1 ส่วนขยาย)ตอนนี้การเรียกใช้
Get-User.ps1
เปิดหน้าต่างคอนโซล ที่พรอมต์คําสั่ง ให้นําทางไปยังโฟลเดอร์ที่คุณบันทึกGet-User.ps1
และป้อนคําสั่งต่อไปนี้:powershell.exe -File .\Get-User.ps1
เมื่อสคริปต์ PowerShell ทํางานได้สําเร็จ ควรพิมพ์รายละเอียดของผู้ใช้ Microsoft Advertising รวมถึงบทบาทลูกค้า สําหรับรายละเอียด ดู GetUser