คําขอทุกรายการที่แอปพลิเคชันส่งไปยัง Manufacturer Center API ต้องมีโทเค็นการให้สิทธิ์ โทเค็นดังกล่าวจะระบุตัวตนแอปพลิเคชันของคุณให้ Google ทราบด้วย
เกี่ยวกับโปรโตคอลการให้สิทธิ์
แอปพลิเคชันต้องใช้ OAuth 2.0 เพื่อให้สิทธิ์คำขอ ระบบไม่รองรับโปรโตคอลการให้สิทธิ์อื่นๆ หากแอปพลิเคชันใช้ลงชื่อเข้าใช้ด้วย Google ระบบจะจัดการการให้สิทธิ์บางส่วนแก่คุณ
การให้สิทธิ์คำขอด้วย OAuth 2.0
คําขอที่ส่งไปยัง Manufacturer Center API ทั้งหมดจะต้องได้รับอนุญาตจากผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว
รายละเอียดของกระบวนการให้สิทธิ์หรือ "ขั้นตอน" ของ OAuth 2.0 จะแตกต่างกันไปโดยขึ้นอยู่กับประเภทของแอปพลิเคชันที่คุณเขียน แอปพลิเคชันทุกประเภทจะใช้กระบวนการทั่วไปต่อไปนี้
- เมื่อสร้างแอปพลิเคชัน คุณจะลงทะเบียนแอปได้โดยใช้คอนโซล Google API จากนั้น Google จะให้ข้อมูลที่คุณต้องใช้ในภายหลัง เช่น รหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์
- เปิดใช้งาน Manufacturer Center API ในคอนโซล Google API (หากไม่เห็น API ดังกล่าวในคอนโซล API ให้ข้ามขั้นตอนนี้ไป)
- เมื่อแอปพลิเคชันต้องการเข้าถึงข้อมูลผู้ใช้ แอปจะขอขอบเขตการเข้าถึงที่จำเป็นจาก Google
- Google จะแสดงหน้าจอขอคำยินยอมแก่ผู้ใช้เพื่อขอให้ผู้ใช้ให้สิทธิ์แอปพลิเคชันของคุณในการขอข้อมูลบางอย่างของผู้ใช้
- หากผู้ใช้อนุมัติ Google จะให้โทเค็นเพื่อการเข้าถึงที่มีอายุชั่วคราวแก่แอปพลิเคชันของคุณ
- แอปพลิเคชันขอข้อมูลผู้ใช้โดยแนบโทเค็นเพื่อการเข้าถึงไปกับคำขอ
- หาก Google ตัดสินว่าคำขอและโทเค็นของคุณถูกต้อง ระบบจะแสดงข้อมูลที่ขอ
บางกระบวนการจะมีขั้นตอนเพิ่มเติม เช่น การใช้โทเค็นการรีเฟรชเพื่อขอโทเค็นเพื่อการเข้าถึงใหม่ ดูข้อมูลอย่างละเอียดเกี่ยวกับกระบวนการของแอปพลิเคชันประเภทต่างๆ ได้ที่เอกสารประกอบเกี่ยวกับ OAuth 2.0 ของ Google
ข้อมูลขอบเขต OAuth 2.0 สําหรับ Manufacturer Center API มีดังนี้
ขอบเขต | ความหมาย |
---|---|
https://www.googleapis.com/auth/manufacturercenter |
สิทธิ์การอ่าน/เขียน |
หากต้องการขอสิทธิ์โดยใช้ OAuth 2.0 แอปพลิเคชันของคุณต้องมีข้อมูลขอบเขต รวมถึงข้อมูลที่ Google ให้คุณตอนลงทะเบียนแอปพลิเคชัน (เช่น รหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์)
เคล็ดลับ: ไลบรารีของไคลเอ็นต์ Google APIs จัดการขั้นตอนการให้สิทธิ์บางส่วนให้คุณได้ ไลบรารีเหล่านี้พร้อมให้บริการเป็นภาษาโปรแกรมต่างๆ โปรดดูรายละเอียดเพิ่มเติมที่หน้าที่มีไลบรารีและตัวอย่าง
ตัวอย่างการให้สิทธิ์
โค้ดต่อไปนี้สาธิตวิธีการกําหนดค่าไคลเอ็นต์และให้สิทธิ์คําขอโดยใช้ OAuth 2.0 สําหรับแอปพลิเคชันที่ติดตั้งไว้ ภาษาอื่นๆ พร้อมให้ใช้งานในหน้าตัวอย่างและไลบรารีของเรา
Java
นี่คือขั้นตอนการให้สิทธิ์สําหรับบรรทัดคําสั่งที่อธิบายไว้ในการใช้ OAuth 2.0 สําหรับแอปพลิเคชันที่ติดตั้ง
ตัวอย่างข้อมูลจากโค้ดตัวอย่าง Java
public static void main(String[] args) {
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
jsonFactory = JacksonFactory.getDefaultInstance();
scopes = "https://www.googleapis.com/auth/manufacturercenter";
// load configuration
File configPath = new File(basePath, "manufacturers");
File configFile = new File(configPath, manufacturers-info.json);
ManufacturersConfig config = new JacksonFactory().fromInputStream(inputStream, ManufacturersConfig.class);
config.setPath(configPath);
// Get authorization token
Credential credential = authenticate(httpTransport, dataStoreFactory, config, jsonFactory, scopes);
// ...
}
}
private static Credential authenticate(httpTransport, dataStoreFactory, config, jsonFactory, scopes) throws Exception {
try {
Credential credential = GoogleCredential.getApplicationDefault().createScoped(scopes);
System.out.println("Loaded the Application Default Credentials.");
return credential;
} catch (IOException e) {
// No need to do anything, we'll fall back on other credentials.
}
if (config.getPath() == null) {
throw new IllegalArgumentException(
"Must use Application Default Credentials with no configuration directory.");
}
File clientSecretsFile = new File(config.getPath(), "client-secrets.json");
if (clientSecretsFile.exists()) {
System.out.println("Loading OAuth2 client credentials.");
try (InputStream inputStream = new FileInputStream(clientSecretsFile)) {
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(jsonFactory, new InputStreamReader(inputStream));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, scopes)
.setDataStoreFactory(dataStoreFactory)
.build();
// authorize
String userID = ConfigDataStoreFactory.UNUSED_ID;
Credential storedCredential = flow.loadCredential(userID);
if (storedCredential != null) {
System.out.printf("Retrieved stored credential for %s from cache.%n", userID);
return storedCredential;
}
LocalServerReceiver receiver =
new LocalServerReceiver.Builder().setHost("localhost").setPort(9999).build();
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize(userID);
System.out.printf("Retrieved credential for %s from web.%n", userID);
return credential;
} catch (IOException e) {
throw new IOException(
"Could not retrieve OAuth2 client credentials from the file "
+ clientSecretsFile.getCanonicalPath());
}
}
throw new IOException(
"No authentication credentials found. Checked the Google Application"
+ "Default Credentials and the paths "
+ clientSecretsFile.getCanonicalPath()
+ ". Please read the accompanying README.");
}