c6436d0e176128729971198bc41b6e8dbbadba9d.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package io.agora.chat;
  2. import io.agora.media.AccessToken2;
  3. public class ChatTokenBuilder2 {
  4. /**
  5. * Build the CHAT user token.
  6. *
  7. * @param appId: The App ID issued to you by Agora. Apply for a new App ID from
  8. * Agora Dashboard if it is missing from your kit. See Get an App ID.
  9. * @param appCertificate: Certificate of the application that you registered in
  10. * the Agora Dashboard. See Get an App Certificate.
  11. * @param userId: The user's id, must be unique.
  12. * optionalUid must be unique.
  13. * @param expire: represented by the number of seconds elapsed since now. If, for example, you want to access the
  14. * Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
  15. * @return The RTC token.
  16. */
  17. public String buildUserToken(String appId, String appCertificate, String userId, int expire) {
  18. AccessToken2 accessToken = new AccessToken2(appId, appCertificate, expire);
  19. AccessToken2.Service serviceChat = new AccessToken2.ServiceChat(userId);
  20. serviceChat.addPrivilegeChat(AccessToken2.PrivilegeChat.PRIVILEGE_CHAT_USER, expire);
  21. accessToken.addService(serviceChat);
  22. try {
  23. return accessToken.build();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. return "";
  27. }
  28. }
  29. /**
  30. * Build the CHAT app token.
  31. *
  32. * @param appId: The App ID issued to you by Agora. Apply for a new App ID from
  33. * Agora Dashboard if it is missing from your kit. See Get an App ID.
  34. * @param appCertificate: Certificate of the application that you registered in
  35. * the Agora Dashboard. See Get an App Certificate.
  36. * @param expire: represented by the number of seconds elapsed since now. If, for example, you want to access the
  37. * Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
  38. * @return The RTC token.
  39. */
  40. public String buildAppToken(String appId, String appCertificate, int expire) {
  41. AccessToken2 accessToken = new AccessToken2(appId, appCertificate, expire);
  42. AccessToken2.Service serviceChat = new AccessToken2.ServiceChat();
  43. serviceChat.addPrivilegeChat(AccessToken2.PrivilegeChat.PRIVILEGE_CHAT_APP, expire);
  44. accessToken.addService(serviceChat);
  45. try {
  46. return accessToken.build();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. return "";
  50. }
  51. }
  52. }