Uploading a file to Google drive using iOS sdk - ios

In my current application, I am trying to upload a .text file to google drive using Google SDK for iOS. But the issue is every time I got the error message in my Log as Insufficient Permission, so If anyone have faced this or have an idea on this type of error kindly help me.
Here is my code,
GTLDriveFile *file = [GTLDriveFile object];
file.descriptionProperty = #"Uploaded from the iOS app.";
file.mimeType = #"text/*";
file.name=#"MyTextFile";
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myTestFile" ofType:#"txt"];
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithFileURL:[NSURL URLWithString:filePath] MIMEType:file.mimeType];
GTLQueryDrive *query = [GTLQueryDrive queryForFilesCreateWithObject:file uploadParameters:uploadParameters];
[self.service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (error) {
NSLog(#"Error: %#", error);
}else{
}
}];
Log print when this action is performed,
unexpected response data (uploading to the wrong URL?)
{"error":{"code":403,"message":"Insufficient Permission","data":[{"domain":"global","reason":"insufficientPermissions","message":"Insufficient Permission"}]},"id":"gtl_3"}
Premature failure: upload-status:"final" location:(null)

I solve this problem
First Step
change your scope to [kGTLAuthScopeDriveFile]
but after first step, i got a same 403 error
because my APP in my phone have scope [kGTLAuthScopeDriveMetadataReadonly]
Second Step
reset my application or build in another simulator
I think first auth key chain using scope readonly was existed so doesn't write file to google drive
after change scope and re-auth then it works
if you change kKeychainItemName's value to another then could re-auth again with new scope
google says in Quickstart tutorial
// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
// private let scopes = [kGTLAuthScopeDriveFile]

Related

Google Drive Api V3 is not returning Web Content link after uploading a new file to Drive?

I am working on google client Api V3 for uploading files from my IOS Application.
The Scopes I have used are
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDrive,kGTLAuthScopeDriveFile,kGTLAuthScopeDriveAppdata,kGTLAuthScopeDriveMetadata, nil];
The code I used for uploading a new file is
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:file.mimeType];
GTLQuery * query = [GTLQueryDrive queryForFilesCreateWithObject:metadata uploadParameters:uploadParameters];
GTLServiceTicket *uploadTicket=[self.gogService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
if (error == nil) {
NSLog(#"File WebContent Link %#", [updatedFile webViewLink]);
}
else {
NSLog(#"An error occurred: %#", [error localizedDescription]);
}
}];
[uploadTicket setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long totalBytesWritten, unsigned long long totalBytesExpectedToWrite)
{
progressBar.progress=(1.0*totalBytesWritten/(1.0*totalBytesExpectedToWrite)) ;
NSLog(#"%llu",totalBytesWritten);
NSLog(#"%llu",totalBytesExpectedToWrite);
}];
The problem is web content link is null and want it in response to store it in my database because my web App will use this link to open that file.
Previous version of google drive api was returning web content link.
How can I get Web Content Link?
Check this documentation: Migrate to Google Drive API v3. Be noted that full resources are no longer returned by default. Use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are return. You may check this sample for reference.

Can we fetch all the photos from the Google Drive

I am asking a general question, Can i fetch all photos from the Google Drive without Downloading process.I am using a Google Drive SDK and i am very stuck for finding the solution.Any one have experience in google drive sdk. Please share your experience.Thanks for any help.
I want only fetch the Google Drive Photos not download in our application.
You can use Drive's query parameters while using the code snippet on the Querying for Files section of the documentation
I checked the Supported MIME Type section, and you can set the q attribute as
NSString *search = #"mimeType = 'application/vnd.google-apps.photo'";
GTLServiceDrive *drive = ...;
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = search;
[drive executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *fileList,
NSError *error) {
if (error == nil) {
NSLog(#"Have results");
// Iterate over fileList.files array
} else {
NSLog(#"An error occurred: %#", error);
}
}];

GTLService not executing query

I have a GTLServiceTicket object aimed to facilitate me to upload files. It is defined earlier in my code as
GTLServiceTicket *_uploadFileTicket;
And called using
NSString *mimeType = [self MIMETypeForFilename:filename
defaultMIMEType:#"video/mp4"];
GTLUploadParameters *uploadParameters =
[GTLUploadParameters uploadParametersWithFileHandle:fileHandle
MIMEType:mimeType];
uploadParameters.uploadLocationURL = locationURL;
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosInsertWithObject:video
part:#"snippet,status"
uploadParameters:uploadParameters];
GTLServiceYouTube *service = self.youTubeService;
_uploadFileTicket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLYouTubeVideo *uploadedVideo,
NSError *error) {
// Callback
NSLog(#"Here!");
//Other uploading file stuff
This used to be working really well. Now the "Here" doesnt even get called. So whatever is happening, its not running that GTLServiceTicket object at all. Its only until very recently that its stopped working. Within that time frame I've implemented two new features:
Google plus sign in
Youtube video searches and gets (on a different view controller).
And heres what I can confirm about my problem:
No error is passed at all, I have an if statement to catch any errors and I cant even get that. Its like its a dud command.
The file location/URL are correct
So my questions are:
How would I go about further investigating this? As in, where would I look within the frameworks to find out where it is going wrong? What trail is this command following (I have NO idea and have tried to find it).
Does anybody have any ideas?
Thanks!

Google Drive API downloadUrl 404 in iOS

So I am trying to allow two users to swap files that each has in their Google Drive. That involves knowing the ID of the other person's file and using the API calls to retrieve it. Both files sit in folders that have been shared to anyone/public.
Trouble is when I execute the code below I am finding that each user can only use the downloadUrl corresponding to the file they own - the others return a 404. In this case either "mine" or "theirs" depending on the account I'm logged into.
// _driveService and its authorizer setup elsewhere
// Retrieve the metadata then the actual data
NSString *mine = #"0B4Pba9IBDsR3T1NVTC1XSGJTenc";
NSString *theirs = #"0B4n9OyY8tqWpNlNaN1dUc3FsNG8";
NSString *get = [NSString stringWithFormat:#"https://www.googleapis.com/drive/v2/files/%#",theirs];
[_driveService fetchObjectWithURL:[NSURL URLWithString:get] completionHandler:^
(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error)
{
if (error != nil)
NSLog(#"Error retrieving metadata: %#", error);
else
{
// Download the actual data
GTMHTTPFetcher *fetcher = [_driveService.fetcherService fetcherWithURLString:file.downloadUrl];
[fetcher beginFetchWithCompletionHandler:^
(NSData *data, NSError *error)
{
if (error != nil)
NSLog(#"Error retrieving actual data: %#", error);
else
{
NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Content: %#", content);
}
}];
}
}];
Error retrieving actual data: Error Domain=com.google.HTTPStatus Code=404 "The operation couldn’t be completed. (com.google.HTTPStatus error 404.)"
What am I doing wrong here? If it's a permissions thing, why am I allowed to get the metadata?
Note this is for an iOS app and both files were created and uploaded from the app using the official client API (rev 353).
Hah, so it seems the devil is in the detail I left out of the question. When creating the authorizer the scope I was providing is kGTLAuthScopeDriveFile, which was the default in an example and I forgot all about it when everything else thus far worked fine. Apparently I need to use kGTLAuthScopeDrive instead (the differences are explained here)
The logic seems a bit flawed here though, I mean I don't want access to other files that weren't created with the app, I just want access to a public file somebody else created with the app...

Assertion failure in GTLService with Google Drive upload

I'm trying to use Google Drive in my iOS app. It authenticates and browses ok, but when I try to upload a file, I get the following error:
*** Assertion failure in -[GTLService uploadFetcherWithRequest:fetcherService:params:](), /google-api-objectivec-client-read-only/Source/Objects/GTLService.m:565
Here's my relevant code:
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:#"application/pdf"];
GTLDriveFile *file = [[GTLDriveFile alloc] init];
file.title = #"test.pdf";
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:file uploadParameters:uploadParameters];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
NSLog(#"Done");
}];
Has anyone run into this problem? Obviously I'm missing something somewhere in the setup I guess but I have no idea what.
Add -ObjC -all_load to the Other Linker Flags setting in the application project's Build Settings.
check above step in https://developers.google.com/drive/quickstart-ios
I browsed through the source and the corresponding line is:
GTL_ASSERT(uploadClass != nil, #"GTMHTTPUploadFetcher needed");
A search for that on stackoverflow led to this answer:
It indicates that the class GTMHTTPUploadFetcher is not linked in to your application.
The class may be missing due to the class file not being linked in the debug or release build target, or due to the preprocessor define GDATA_INCLUDE_YOUTUBE_SERVICE not being set, as described under "Removing Unneeded Code" at http://code.google.com/p/gdata-objectivec-client/wiki/BuildingTheLibrary

Resources