Google Drive API IOS Permissions of GTLRDriveService - objective-c

I am playing around with the google drive API and trying to build a simple app that uploads a picture to my google drive. The app is supposed to upload a picture once the user is signed in, however it gives an error of
"2017-09-14 00:55:20.342237-0400 driveTest[6705:1647551] An error
occurred: Error Domain=com.google.GTLRErrorObjectDomain Code=403
"Insufficient Permission"
UserInfo={GTLRStructuredError=GTLRErrorObject 0x1c4251d30:
{message:"Insufficient Permission" errors:[1] code:403},
NSLocalizedDescription=Insufficient Permission}"
I have tried to pass it the service which is of type GTLRDriveService to the initSetup() function of the userSetUp class, but to no avail. Could someone please point me to the right track as to why my permissions are not working even though I have logged on correctly, and the part where I am passing in the GTLRDriveService is in the code that runs after a sucessful login.
I instantiate a userSetUp object and I
let setUpUser = userSetUp()
setUpUser.initSetup(service)
I have userSetUp written in objective c as such and it is bridged correctly as I am able to instantiate it in my viewcontroller file which is written in swift.
UserSetUp:::::::
#import "userSetUp.h"
#import <GoogleSignIn/GoogleSignIn.h>
#import GoogleAPIClientForREST;
#implementation userSetUp
- (void) initSetup:(GTLRDriveService *) driveService {
printf("heloooooaiosuoiadoidauoalo");
//GTLRDriveService *driveService = [GTLRDriveService new];
//NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:#"files/apple.jpg"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"apple" ofType:#"jpg"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
GTLRDrive_File *metadata = [GTLRDrive_File object];
metadata.name = #"apple.jpg";
//metadata.mimeType = #"application/vnd.google-apps.document";
GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData:fileData
MIMEType:#"image/jpeg"];
uploadParameters.shouldUploadWithSingleRequest = TRUE;
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
uploadParameters:uploadParameters];
query.fields = #"id";
[driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
GTLRDrive_File *file,
NSError *error) {
if (error == nil) {
//NSLog(#"File ID %#", file.identifier);
printf("it worked");
} else {
NSLog(#"An error occurred: %#", error);
}
}];
printf("upload complete!");
}
#end
And Viewcontroller. swift
import GoogleAPIClientForREST
import GoogleSignIn
import UIKit
class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
private let scopes = [kGTLRAuthScopeDriveReadonly]
let service = GTLRDriveService()
let signInButton = GIDSignInButton()
let output = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
// Configure Google Sign-in.
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().scopes = scopes
GIDSignIn.sharedInstance().signInSilently()
signInButton.frame = CGRect(x: view.frame.width/2 - signInButton.frame.width , y: view.frame.height/2, width: signInButton.frame.width, height: signInButton.frame.height)
// Add the sign-in button.
view.addSubview(signInButton)
// Add a UITextView to display output.
output.frame = view.bounds
output.isEditable = false
output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
output.autoresizingMask = [.flexibleHeight, .flexibleWidth]
output.isHidden = true
view.addSubview(output);
//let itsASetup()
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
showAlert(title: "Authentication Error", message: error.localizedDescription)
self.service.authorizer = nil
} else {
self.signInButton.isHidden = true
self.output.isHidden = false
self.service.authorizer = user.authentication.fetcherAuthorizer()
listFiles()
}
}
// List up to 10 files in Drive
func listFiles() {
let query = GTLRDriveQuery_FilesList.query()
query.pageSize = 10
service.executeQuery(query,
delegate: self,
didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:))
)
}
// Process the response and display output
#objc func displayResultWithTicket(ticket: GTLRServiceTicket,
finishedWithObject result : GTLRDrive_FileList,
error : NSError?) {
if let error = error {
showAlert(title: "Error", message: error.localizedDescription)
return
}
var text = "";
if let files = result.files, !files.isEmpty {
text += "Files:\n"
for file in files {
text += "\(file.name!) (\(file.identifier!))\n"
}
} else {
text += "No files found."
}
output.text = text
let setUpUser = userSetUp()
setUpUser.initSetup(service)
}
// Helper for showing an alert
func showAlert(title : String, message: String) {
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert
)
let ok = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.default,
handler: nil
)
alert.addAction(ok)
present(alert, animated: true, completion: nil)
}
}

try to Change your scope like:
class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate
{
// If modifying these scopes, delete your previously saved credentials by
private let scopes = ["https://www.googleapis.com/auth/drive"]
...
}

Related

PromiseKit Framework - Ambiguous reference to member 'then()'

I am using a PromiseKit framework version 1.7.7 (I need to use this version due another framework that needs it).
So, in this framework that uses the PromiseKit framework has this method:
- (PMKPromise *)paymentTokenForCreditCard:(GNCreditCard *)creditCard {
NSDictionary *cardDict = [creditCard paramsDicionary];
NSString *jsonCard = [self getJSONStringFromDictionary:cardDict];
return [self encryptData:jsonCard]
.then(^(NSString *encryptedData){
NSDictionary *params = #{#"data":encryptedData};
return [self request:kGNApiRouteSaveCard method:#"POST" params:params];
})
.then(^(NSDictionary *response){
return [[GNPaymentToken alloc] initWithDictionary:response];
});
}
And it shows this example on how to use it:
GNConfig *gnConfig = [[GNConfig alloc] initWithAccountCode:#"YOUR_ACCOUNT_CODE" sandbox:YES];
GNApiEndpoints *gnApi = [[GNApiEndpoints alloc] initWithConfig:gnConfig];
GNCreditCard *creditCard = [[GNCreditCard alloc] init];
creditCard.number = #"4012001038443335";
creditCard.brand = kGNMethodBrandVisa;
creditCard.expirationMonth = #"05";
creditCard.expirationYear = #"2018";
creditCard.cvv = #"123";
[gnApi paymentTokenForCreditCard:creditCard]
.then(^(GNPaymentToken *paymentToken){
NSLog(#"%#", paymentToken.token);
})
.catch(^(GNError *error){
NSLog(#"An error occurred: %#", error.message);
});
Well, how I am using Swift instead of Object-C, I am trying to use it this way:
let gnConfig = GNConfig(accountCode: "3f62976bea79971730b67cd62806c256", sandbox: true)
let gnEndpoints = GNApiEndpoints(config: gnConfig)
let gnCreditCard: GNCreditCard! = GNCreditCard(number: "4012001038443335", brand: kGNMethodBrandVisa, expirationMonth: "05", expirationYear: "2018", cvv: "123")
gnEndpoints?.paymentToken(for: gnCreditCard).then({ tokenPagamento in
if let aToken = tokenPagamento?.token {
print("\(aToken)")
}
}).catch({ error in
if let aMessage = error?.message {
print("An error occurred: \(aMessage)")
}
})
And it is showing me this error:
Ambiguous reference to member 'then()'
How could I fix it?
Try this code below. The promise thing should work. Modify it upon your needs.
gnEndpoints?.paymentToken(for: gnCreditCard).then({ token -> () in
if let token = token as? GNPaymentToken {
print(token)
}
}, { (error) -> () in
// catch your error
})

How to make HTTP post request with specific body? And how to access token from the server to allow me to login?

I'm having a scenario like :
1) I want to create Http POST request and for this I'm having the data, please see this image:
2) As you can see in the above image, I have to create post a request with the mentioned body and also I'm getting response named: token. How to create post request and fetch this token response?.
3) That token response will allow me to login into myapp.
I'm newbie to this scenario. I have tried some code by my own but still getting confuse in how to combine my app delegate code with this POST Request Code.
Code
#IBAction func signinaction(_ sender: Any) {
self.username.resignFirstResponder()
self.password.resignFirstResponder()
if (self.username.text == "" || self.password.text == "") {
let alertView = UIAlertController(title: "Login failed",
message: "Wrong username or password." as String, preferredStyle:.alert)
let okAction = UIAlertAction(title: "Try Again!", style: .default, handler: nil)
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
return
}
// Check if the user entered an email
if let actualUsername = self.username.text {
// Check if the user entered a password
if let actualPassword = self.password.text {
// Build the body message to request the token to the web app
self.bodyStr = "username=8870417698&password=1234&grant_type=password" + actualUsername + "&password=" + actualPassword
// Setup the request
let myURL = NSURL(string: "http://ezschoolportalapi.azurewebsites.net/token")!
let request = NSMutableURLRequest(url: myURL as URL)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = bodyStr.data(using: String.Encoding.utf8)!
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) -> Void in
if data?.count != 0
{
do {
let tokenDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! NSDictionary
print(tokenDictionary)
// Get the token
let token:String = tokenDictionary["access_token"] as! String
// Keep record of the token
let userdefaults = UserDefaults()
let saveToken = userdefaults.set(token, forKey: "access_token")
userdefaults.synchronize()
// Dismiss login view and go to the home view controller
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
catch {
// Wrong credentials
// Reset the text fields
self.username.text = ""
self.password.text = ""
// Setup the alert
let alertView = UIAlertController(title: "Login failed",
message: "Wrong username or password." as String, preferredStyle:.alert)
let okAction = UIAlertAction(title: "Try Again!", style:.default, handler: nil)
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
return
}
}
}
task.resume()
}
}
}
Question is how to combine this code with my above code :
let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.gotoMainvc()
if I use directly this code then in any of the case I'm able to switch over to my home screen it doesn't matter whether m using this POST Request code or not. Please Help.
You are hard coding the username and password in bodyStr update it to
self.bodyStr = "username=" + actualUsername + "&password=" + actualPassword + "&grant_type=password"
Update the statements inside do with
do {
let tokenDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! NSDictionary
print(tokenDictionary)
// Get the token
if let authToken = tokenDictionary["access_token"] as? String{
self.token = authToken
UserDefaults.standard.set(accessToken, forKey: "access_token")
UserDefaults.standard.synchronize()
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
}
You can implement a function like isUserLoggedIn which will return true if token is saved in userDefaults. You need to check whether the user has logged in in the appDelegate's applicationdidFinishLaunchingWithOptions.Like
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if isUserLoggedIn(){
//showHomeViewController
} else{
//showLoginViewController
}
return true
}
func isUserLoggedIn() -> Bool{
if let accessToken = UserDefaults.standard.object(forKey: "access_token") as? String {
if (accessToken.characters.count)! > 0{
return true
} else {
return false
}
}
else {
return false
}
}

GTMAppAuth + MailCore: "A stable connection to the server could not be established."

under iOS using GMTAuth to access gmail. The test code follows below. The account is accessible with MailCore, essentially similar code, but user/pw no oAuth. Any clues would be appreciated.
I get this log output:
__nw_connection_get_connected_socket_block_invoke 3 Connection has no connected handler
2017-02-24 17:20:02.977 Example-iOS[38329:24679819] finished checking account.
2017-02-24 17:20:10.526 Example-iOS[38329:24679819] error loading account: Error Domain=MCOErrorDomain Code=1 "A stable connection to the server could not be established."
and this is the code:
self.imapSession = [[MCOIMAPSession alloc] init];
self.imapSession.hostname = #"imap.google.com";
self.imapSession.port = 993;
self.imapSession.username = [_authorization userEmail];
GTMAppAuthFetcherAuthorization* authorization =
[GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kExampleAuthorizerKey];
self.imapSession.OAuth2Token = authorization.authState.lastTokenResponse.accessToken;
self.imapSession.authType = MCOAuthTypeXOAuth2;
self.imapSession.connectionType = MCOConnectionTypeTLS;
GTMAppAuthExampleViewController * __weak weakSelf = self;
self.imapSession.connectionLogger = ^(void * connectionID, MCOConnectionLogType type, NSData * data) {
#synchronized(weakSelf) {
if (type != MCOConnectionLogTypeSentPrivate) {
NSLog(#"event logged:%p %li withData: %#", connectionID, (long)type, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
}
};
// Reset the inbox
self.messages = nil;
NSLog(#"checking account");
self.imapCheckOp = [self.imapSession checkAccountOperation];
[self.imapCheckOp start:^(NSError *error) {
GTMAppAuthExampleViewController *strongSelf = weakSelf;
NSLog(#"finished checking account.");
if (error == nil) {
// TBD: [strongSelf loadLastNMessages:NUMBER_OF_MESSAGES_TO_LOAD];
} else {
NSLog(#"error loading account: %#", error);
}
strongSelf.imapCheckOp = nil;
}];
}
Update: I figured out the solution. Here's my full working code:
func loadAccount() {
// This is just a model file holding the account details...
let account = emailAccounts[currentAccount]
imapSession = MCOIMAPSession()
imapSession?.hostname = account.hostname
imapSession?.port = 993
imapSession?.username = account.userName
imapSession?.authType = .xoAuth2
// This email helper code is from this guide:
// https://github.com/MailCore/mailcore2/wiki/Implementing-OAuth-2.0
imapSession?.oAuth2Token = EmailHelper.singleton().authorization?.authState.lastTokenResponse?.accessToken
// This is important too (wasn't working with other values)
imapSession?.connectionType = .TLS
imapSession?.connectionLogger = { [unowned self] (connectionID, type, data) in
let lockQueue = DispatchQueue(label: "com.inboxzero.LockQueue")
lockQueue.sync() {
if (type != MCOConnectionLogType(rawValue: NSInteger(2))) { // MCOConnectionLogTypeSentPrivate
if let theData = data, let str = connectionID {
let theString = String(describing: theData)
print("event logged:\(str) \(type) withData: \(theString)")
}
}
}
}
// Reset the inbox:
messages = nil
isLoading = false
print("checking account \(self.imapSession!.username!)")
imapCheckOp = self.imapSession?.checkAccountOperation()
if let checkOp = imapCheckOp {
checkOp.start( { (error) in
print("finished checking \(self.imapSession!.username!)")
if (error == nil) {
self.loadLastNMessages(nMessages: self.NUMBER_OF_MESSAGES_TO_LOAD)
}
else {
print("error loading account \(self.imapSession!.username!): %#", error!)
self.alertWithTitle(title:"Mail Core", message:error.debugDescription)
return
}
})
}
}

How to set User Profile Image In Quickblox objective c

[QBRequest signUp: user successBlock: ^ (QBResponse * response, QBUUser * user) {
// your file - this is an image in our case
NSData * imageData = UIImageJPEGRepresentation([UIImage imageNamed: #"Profile Picture"], 0.8 f);
[QBRequest TUploadFile: imageData fileName: #"ProfilePicture"
contentType: #"image/jpeg"
isPublic: YES successBlock: ^ (QBResponse * response, QBCBlob * blob) {
// File uploaded, do something
// if blob.isPublic == YES
NSString * url = [blob publicUrl];
}
statusBlock: ^ (QBRequest * request, QBRequestStatus * status) {
// handle progress
}
errorBlock: ^ (QBResponse * response) {
NSLog(#"error: %#", response.error);
}
];
I have been trying to sing up a user, and upload an profile image but can not figure out the right way to do it. Do I have to upload it to custom data, or content module??? I already have a field created in CustomData. The code is running the file is getting saved somewhere but i don know where.
What am I missing, please help>about to go crazy>>>thank you
After you uploaded your picture (TUploadFile), in successBlock set [blob ID] to your QBUUser blobID property. After that just perform update user:
QBUpdateUserParameters *params = [QBUpdateUserParameters new];
params.blobID = [blob ID];
[QBRequest updateCurrentUser:params successBlock:^(QBResponse * _Nonnull response, QBUUser * _Nullable user) {
// success block
} errorBlock:^(QBResponse * _Nonnull response) {
// error block
NSLog(#"Failed to update user: %#", [response.error reasons]);
}];
Swift 4.2 :
This answer is for those who want to set/get user profile image from url (set url of image)
we can use customData property of QBUUser class. it will hold a String.
Set :
set customData while signUp.
let newUser = QBUUser()
newUser.login = "Saifan"
newUser.fullName = "Nadaf"
//Set your image url string here
newUser.customData = "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"
newUser.password = "Pass#1234"
QBRequest.signUp(newUser, successBlock: { [weak self] response, user in
guard let self = self else {
return
}
//SignUp Sucessfull Call Login Method Here
}, errorBlock: { [weak self] response in
response.status.rawValue == 422 {
// The User With Existent Login Was Created Earlier
// Call Login Method Here
return
}
})
if we want to set/Update for existing users we can update user profile using QBUpdateUserParameters()
let updateUserParameter = QBUpdateUserParameters()
//Set ur Image URL to customData
updateUserParameter.customData = "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"
//Use updateCurrentUser() to update user parameters
QBRequest.updateCurrentUser(updateUserParameter, successBlock: { [weak self] response, user in
guard let self = self else {
return
}
//*** This is Success Block ***//
//variable user is current updated User you can check on admin panel also
print("updated custom data \(user.customData)")
//Connect to chat using QBChat.instance.connect()
}, errorBlock: { [weak self] response in
//Handle Error Here
})
Get :
fetch users profile image using recipientID from QBChatDialog class if Dialog is created.
QBRequest.user(withID: UInt(chatDialog.recipientID), successBlock: { (res, user) in
if let imgUrl = user.customData {
// Here we will get imageUrl
print(imgUrl)
} else {
// Set Default image here
}
}) { (response) in }
fetch user profile image using customData from QBUUser class
if let imgrUrl = user.customData{ // here user is a QBUUser()
// Here we will get imageUrl
print(imgUrl)
} else {
// Set Default image here
}
Check on admin panel leftSide->user->ID->

Objective-C to Swift: Facebook iOS API requestUserInfo method

Hi I am trying to figure out how to rewrite this in swift:
- (IBAction)requestUserInfo:(id)sender
{
// We will request the user's public picture and the user's birthday
// These are the permissions we need:
NSArray *permissionsNeeded = #[#"public_profile", #"user_birthday", #"email"];
// Request the permissions the user currently has
[FBRequestConnection startWithGraphPath:#"/me/permissions" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
// Parse the list of existing permissions and extract them for easier use
NSMutableArray *currentPermissions = [[NSMutableArray alloc] init];
NSLog(#"The fucking class is: %#", [result class]);
NSArray *returnedPermissions = (NSArray *)[result data];
for (NSDictionary *perm in returnedPermissions) {
if ([[perm objectForKey:#"status"] isEqualToString:#"granted"]) {
[currentPermissions addObject:[perm objectForKey:#"permission"]];
}
} // cut cut here
}
EDIT:
I was having trouble trying to get the required data out of the FBGraphObject but figured it out after some further inspection. I have posted the swift version below so that people can just cut and paste it and get on with using swift. Hope it saves someone some time.
Here:
#IBAction func requestUserInfo(sender: AnyObject){
// These are the permissions we need:
var permissionsNeeded = ["public_profile", "user_birthday", "email"]
// Request the permissions the user currently has
FBRequestConnection.startWithGraphPath("/me/permissions", completionHandler: {(connection, result, error) -> Void in
if error == nil{
// Parse the list of existing permissions and extract them for easier use
var theResult = result as? [String:[AnyObject]]
var currentPermissions = [String]()
let returnedPermissions = theResult?["data"] as [[String:String]]
for perm in returnedPermissions {
if perm["status"] == "granted" {
currentPermissions.append(perm["permission"]!)
}
}
// Build the list of requested permissions by starting with the permissions
// needed and then removing any current permissions
println("Needed: \(permissionsNeeded)")
println("Current: \(currentPermissions)")
var requestPermissions = NSMutableArray(array: permissionsNeeded, copyItems: true)
requestPermissions.removeObjectsInArray(currentPermissions)
println("Asking: \(requestPermissions)")
// TODO PUT A POPUP HERE TO TELL WHAT PERMISSIONS WE NEED!
// If we have permissions to request
if requestPermissions.count > 0 {
// Ask for the missing permissions
FBSession.activeSession().requestNewReadPermissions(requestPermissions, completionHandler: {(session, error) -> Void in
if (error == nil) {
// Permission granted, we can request the user information
self.makeRequestForUserData()
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
println("error: \(error?.description)")
}
})
} else {
// Permissions are present
// We can request the user information
self.makeRequestForUserData()
}
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
println("error: \(error?.description)")
}
})
}
private func makeRequestForUserData() {
FBRequestConnection.startForMeWithCompletionHandler({(connection, result, error) -> Void in
if (error == nil) {
// Success! Include your code to handle the results here
println("user info: \(result)")
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
println("error: \(error?.description)")
}
})
}
// Show an alert message
func showMessage(text : NSString, title : NSString){
var alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().delegate?.window!?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}