ไม่พบสเปรดชีตเมื่อใช้ AppIdentityService ในการตรวจสอบสิทธิ์

ฉันสามารถตรวจสอบแอป App Engine ของฉันได้สำเร็จโดยใช้ AppIdentityService อย่างไรก็ตาม เมื่อฉันดำเนินการ SpreadsheetService.getEntries ฉันจะไม่ได้รับรายการใดๆ

นี่คือรหัสของฉัน:

SpreadsheetService service = new SpreadsheetService("Spreadsheet editing");
String[] SCOPESArray = { "https://spreadsheets.google.com/feeds" };
final List SCOPES = Arrays.asList(SCOPESArray);
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(SCOPES);

Credential creds = new Credential(BearerToken.authorizationHeaderAccessMethod());
creds.setAccessToken(accessToken.getAccessToken());    
service.setOAuth2Credentials(creds);

SpreadsheetFeed feed = service.getFeed(new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"), SpreadsheetFeed.class);

List spreadsheets = feed.getEntries();

ณ จุดนี้ สเปรดชีตจะเป็นรายการว่าง

ฉันได้แชร์สเปรดชีตกับแอปของฉันโดยป้อนอีเมลของบัญชีบริการในช่อง "แชร์" ของสเปรดชีต - นี่คืออีเมลในรูปแบบ: [email protected]

ฉันเข้าใจผิดการอนุญาตที่นี่หรือไม่? การ "แชร์" สเปรดชีตกับบัญชีบริการไม่เพียงพอที่จะทำให้บัญชีบริการ "เห็น" สเปรดชีตหรือไม่


person Dan Gravell    schedule 29.05.2015    source แหล่งที่มา


คำตอบ (2)


ในขณะนี้ ฉันเชื่อว่า AppIdentity ไม่ได้รับการสนับสนุนเมื่ออัปเดตโดยใช้ Sheets API จาก Google App Engine

ดูเหมือนว่าจะสามารถตรวจสอบสิทธิ์ได้ แต่รายการ SpreadsheetEntrys จะว่างเปล่าเสมอ

ให้ใช้บัญชีบริการจาก Google App Engine แทนโดยระบุข้อมูลรับรองในอินสแตนซ์ GoogleCredential:

GoogleCredential creds = new GoogleCredential.Builder()
    .setTransport(GoogleNetHttpTransport.newTrustedTransport())
    .setJsonFactory(JacksonFactory.getDefaultInstance())
    .setServiceAccountId(serviceAccountEmailAddress)
    .setServiceAccountPrivateKeyFromP12File(p12FileFromCredentials)
    .setServiceAccountScopes(SCOPES)
    .build();


service.setOAuth2Credentials(creds);
person Dan Gravell    schedule 01.06.2015

ฉันพบว่าการแบ่งปันทำงานได้ไม่ดีนักบน Google เอกสาร กล่าวคือ โฟลเดอร์ "แบ่งปันกับฉัน" ค่อนข้างไม่สมบูรณ์ ดูเหมือนว่าจะได้รับการปรับปรุงด้วย Drive แต่ Spreadsheet API นั้นโบราณ...

สิ่งที่ฉันทำคือแชร์โฟลเดอร์ที่มีสเปรดชีตกับบัญชีบริการ App Engine และใช้ Drive API เพื่อแสดงรายการสเปรดชีตในโฟลเดอร์เหล่านี้ เข้าถึงโฟลเดอร์ได้ด้วยรหัส ซึ่งช่วยให้ฉันสามารถกำหนดค่าโฟลเดอร์ต่างๆ สำหรับการทดสอบหน่วย การทดสอบ และการใช้งานจริงได้

ต่อไปนี้คือโค้ดตัวอย่างบางส่วนในการรับสเปรดชีตจากโฟลเดอร์:

public enum MimeType { //
  SPREADSHEET ("application/vnd.google-apps.spreadsheet"),
  EXCEL ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

  private String type;
  MimeType(String type) {this.type = type;}

  public String getType() {
    return type;
  }
}

/**
 * Get a list of files of a given type from a folder 
 * 
 * @param folderId The Id of the folder
 * @param type The mime type of the files
 * @return a {@link FileList} containing the desired files
 * @throws IOException
 */
public FileList getFolderItems(String folderId, MimeType type) throws IOException {
    QueryBuilder query = new QueryBuilder();
    query.addParent(folderId).setMimeType(type);
    FileList list = drive.files().list().setQ(query.getQuery()).execute();
    return list;
}

จากนั้นคุณสามารถใช้ File.getId() เพื่อดึงข้อมูลคีย์ไฟล์เพื่อใช้กับ Spreadsheet API

นอกจากนี้ ฉันไม่แนะนำให้ใช้ API สเปรดชีตจาก App Engine API ทำงานเข้าสู่ระยะหมดเวลาและข้อยกเว้นอื่นๆ อย่างต่อเนื่องเมื่อเรียกจาก GAE ฉันต้องย้ายการประมวลผลของฉันไปอยู่ในคิวงานซึ่งน่าเสียดายที่ไม่มีการหมดเวลา URLFetch อีกต่อไปตามที่สัญญาไว้ (ดู ฉบับที่ 10470) แต่อย่างน้อย คำขอจะไม่ถูกยกเลิก

ฉันเขียน wrapper ไว้รอบๆ การเรียก API ของ Spreadsheet ส่วนใหญ่เพื่อลองโทรอีกครั้งเมื่อได้รับข้อยกเว้น และฉันก็เพิ่มกระดาษห่อเพิ่มเติมอย่างต่อเนื่อง

person Alex R    schedule 30.05.2015
comment
ขอบคุณ แต่ฉันยังไม่สามารถใช้งาน AppIdentity ได้ ท้ายที่สุดแล้ว การสลับไปใช้บัญชีบริการที่มีชื่อด้วย GoogleCredential เป็นสิ่งที่ได้ผล - person Dan Gravell; 01.06.2015
comment
ฉันใช้ลำดับคำสั่งเดียวกันเพื่อทำงานกับ Spreadsheet API ด้วย AppIdentity คุณลองใช้คำสั่งอื่นโดยใช้คีย์ไฟล์ที่กำหนดใน API หรือไม่ การเชื่อมต่อไดรฟ์ของฉันสร้างขึ้นด้วยข้อมูลประจำตัวอื่นโดยใช้ AppIdentityCredential.Builder - person Alex R; 01.06.2015
comment
สวัสดีอเล็กซ์ ขอบคุณสำหรับข้อมูลของคุณ ควรใช้ Drive API หากเป็นไปได้จะดีกว่า หนึ่งคำถาม; คุณจะดึงสเปรดชีตด้วย File.getId() ได้อย่างไร คุณมีตัวอย่างหรือไม่? ขอแสดงความนับถือโรแลนด์ - person Roland Beuker; 04.03.2016
comment
@Roland Beuker ฉันใช้ urlFactory.getWorksheetFeedUrl(file.getId(), "private", "full") (หรือตัวสร้าง URL ฟีดอื่น ๆ ) file.getId() ส่งคืนคีย์สเปรดชีต - person Alex R; 04.03.2016
comment
@Alex ด้วยการดีบักฉันพบว่า:String lSpreadsheetFileId = pSpreadsheetFile.getId(); String lSpreadsheetUrlString = String.format("https://spreadsheets.google.com/feeds/spreadsheets/%s", lSpreadsheetFileId); URL lSpreadsheetUrl = new URL(lSpreadsheetUrlString); SpreadsheetEntry lSpreadsheetEntry = = mSpreadsheetService.getEntry(lSpreadsheetUrl, SpreadsheetEntry.class); แต่คุณจะใช้ WorksheetFeedUrl เพื่อรับ SpreadsheetEntry ได้อย่างไร - person Roland Beuker; 04.03.2016
comment
@Roland Beuker ฉันไม่; ฉันควรทำอย่างไรกับ SpreadhsheetEntry สิ่งเดียวที่ทำได้คือส่งใบงานให้ฉัน ซึ่งฉันดึงมาจาก WorksheetFeed - person Alex R; 07.03.2016
comment
ฉันสร้างไลบรารีที่มีความเป็นไปได้ที่จะทำงานกับไฟล์ SpreadsheetService และ GoogleDrive ด้วยวิธีแก้ปัญหาข้างต้น ข้อแตกต่างเพียงอย่างเดียวคือวิธีที่ฉันได้รับ SpreadsheetEntry จากคลาสระดับบนสุดนี้ โค้ดทั้งหมดเป็นแบบทั่วไป ... ฉันชอบ SpreadsheetEntry ด้วยเพราะมันจำลองสเปรดชีตหนึ่งอันว่ามันคืออะไรในโลกแห่งความเป็นจริง - person Roland Beuker; 07.03.2016
comment
@Roland Beuker โพสต์คำถามและฉันสามารถเพิ่มโค้ดตัวอย่างได้ - person Alex R; 07.03.2016
comment
@Alex ขอบคุณ นี่คือลิงก์คำถามของฉัน: stackoverflow.com/ คำถาม/35863845/ - person Roland Beuker; 08.03.2016