如何解决尝试访问 iOS 应用中的 gmail 收件箱时,Swift 权限被拒绝
我有一个可以通过 GIDSignIn 登录的 iOS 应用。该项目最初是通过 Firebase 创建的,因此它还包含 GoogleInfo.plist 信息。
我正在尝试从用户的 Gmail 收件箱中访问邮件,我一直在通过以下示例进行操作:https://www.devfright.com/getting-a-list-of-messages-with-the-gmail-api/
我继续遇到的问题是,我收到 403 错误,指出权限未被拒绝,并且 OATH2 需要一个登录身份验证令牌,该令牌本应从 GIDSignInUser 对象提供。
我的谷歌开发者控制台也启用了 Gmail API
下面是我项目中的代码实现
应用委托
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
//check user sign in
WebService.shared.silentlySignInUser()
return true
}
网络服务
class WebService: NSObject {
static let shared = WebService()
let baseURL = ""
private let scopes = [kGTLRAuthScopeGmailReadonly]
let googleDriveService = GTLRGmailService()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var googleUser: GIDGoogleUser?
override init() {
super.init()
GIDSignIn.sharedInstance()?.delegate = self
GIDSignIn.sharedInstance()?.scopes = scopes
}
func silentlySignInUser() {
//safely unwrap optional from gidsignin shared instance
if let alreadySignedIn = GIDSignIn.sharedInstance()?.hasPrevIoUsSignIn() {
//check if user has already signed in
if alreadySignedIn {
//sign in user with prevIoUs credentials
GIDSignIn.sharedInstance()?.restorePrevIoUsSignIn()
//load user object in global singleton
if let loadedUser = GlobalSingleton.shared.load_google_user() {
GlobalSingleton.shared.gUser = loadedUser
//appDelegate.startApplication()
}
} else {
print("has not signed in yet")
}
} else {
GIDSignIn.sharedInstance()?.signIn()
}
}
}
extension WebService: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!,didSignInFor user: GIDGoogleUser!,withError error: Error!) {
// A nil error indicates a successful login
if error == nil {
// Include authorization headers/values with each Drive API request.
self.googleUser = user
loginToFirebase(user)
} else {
self.googleDriveService.authorizer = nil
self.googleUser = nil
}
// ...
}
private func loginToFirebase(_ googleUser: GIDGoogleUser) {
guard let authentication = googleUser.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,accesstoken: authentication.accesstoken)
//sign in to firebase
Auth.auth().signIn(with: credential) { (authResult,error) in
if let error = error {
print(error)
} else {
//user has been successfully signed in to firebase database
let defaultString = ""
let user_first_name = googleUser.profile.givenname ?? defaultString
let user_last_name = googleUser.profile.familyName ?? defaultString
let user_email = googleUser.profile.email
guard user_email != nil else {
//show error stating that user email is not a valid google email
return
}
let newUserObj = UserObj(first_name: user_first_name,last_name: user_last_name,email: user_email!)
GlobalSingleton.shared.save_google_user(newUserObj)
//if authentication passes then start application
self.appDelegate.startApplication()
}
return
}
}
func sign(_ signIn: GIDSignIn!,diddisconnectWith user: GIDGoogleUser!,withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
func listInBoxMessages() {
let gmailService = GTLRGmailService()
//authenticated user's email address
let authorizer = googleUser?.authentication.fetcherAuthorizer()
self.googleDriveService.authorizer = authorizer
let listQuery = GTLRGmailQuery_UsersMessagesList.query(withUserId: "me")
//populate with all messages that contain "INBox" id
listQuery.labelIds = ["INBox"]
gmailService.executeQuery(listQuery) { (ticket,response,error) in
if response != nil {
print("Response: ")
print(response)
} else {
print("Error: ")
print(error)
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。