Setting up YouTube access within app, stuck on retrieving url response - ios

I can't find any answers or similar questions to my YouTube API puzzle and hoping someone can help.
My iOS app, which makes a video within it, needs to connect to a user's YouTube account so they can upload the video for others to see etc.
I've followed the API documentation and managed to connect to the authentication part where the user logs into their account, (code I used posted below).
It works in the sense it takes user to the login page for Google, when logged in the page asks if the user would like to authorise the app access to account, etc. When the user accepts though, I understand there should be data returned which gives the app authentication, the token code, so it can be used to upload videos later within the app.
My question is.... How do I retrieve this data? When I click accept it simply takes me to another webpage, but I don't know where to retrieve the info.
Thanks in advance for anyone who can point me in the right direction.
#define kAuthUrl #"https://accounts.google.com/o/oauth2/auth?"
#define kClientID #"client_id=xxxxxxxxxxxMyUniqueIDDetailsxxxxxxxxxx&"
#define kRediretUri #"redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
#define kScopeUL #"scope=https://www.googleapis.com/auth/youtube.upload&"
#define kResponse #"response_type=code"
Above code placed in header file.
- (void)viewDidLoad
{
NSString *loginURL = [NSString stringWithFormat:#"%#%#%#%#%#", kAuthUrl, kClientID, kRediretUri, kScopeUL, kResponse];
NSURLRequest * urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString:loginURL]];
[_webView loadRequest:urlReq];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
From the YouTube API documentation it says
When this value is used, your application can sense that the page has loaded and the title of the HTML page contains the authorization code. It is then up to your application to close the browser window if you want to ensure that the user never sees the page that contains the authorization code. The mechanism for doing this varies from platform to platform.
Where "this value" refers to the redirect_uri I chose, as opposed to a local host redirect.
How can I sense the user has logged on to their account and pressed the "accept" button, and this now also leads me on to asking how to know this and then retrieve the code from the returned url while closing the browser.
Cheers, Jim.

My strong recommendation is to use the Google Toolbox for Mac OAuth 2 Controllers project to handle all the details of your OAuth 2 integration for you.

Related

iOS Square Connect Merchant Authorization Issue

I'm having trouble authorizing a merchant in the Square OAuth flow and am using AFNetworking 2.0 in Objective-C. I called the method 'openURL' which brought up the Square log in page. From here I can log in as a merchant, but then it never returns to the app. Recommendations and code examples on what I need to do to get this to work would be much appreciated as I am very lost here. Thanks in advance!
OPEN URL Call
The 'openURL' call successfully redirects the user to 'https://connect.squareup.com/oauth2/authorize' with the Client ID and Redirect URL on Safari. After logging in and pressing "Allow" for the permissions, it doesn't jump back to the app. The code I tried follows:
NSString *urlString= #"https://connect.squareup.com/oauth2/authorize";
NSString *clientID = #"client_id=[CLIENT_ID]";
NSString *responseType = #"response_type=code";
NSString *fullURL = [NSString stringWithFormat:#"%#?%#&%#", urlString, clientID, responseType];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:fullURL]];
I am thinking it may have something to do with the URLSchemes and URL identifier. Should the "Redirect URL" on the Square portal be the same as the URL Scheme or the URL identifier? I tried both and it didn't work.
As an example, would the following values in my Info.plist be correct? My URL Schemes single item is "iostestapp" and my URL identifier is "com.name.iostestapp". The "Redirect URL" in the Square portal is "https://com.name.iostestapp".
Is something else besides the code I provide needed? I've been seeing a lot about handlers concerning OAuth online but don't see that in the Square docs.
It is necessary for the user to authorize your developer application to access their data via the API. In order to do so, you must guide the user through the Square provided Oauth flow. It is not sufficient to programmatically perform a GET request to this URL. You must guide the user through the oauth flow in a web browser.
You can present the Oauth flow to your users by opening a web page with the following URL: https://connect.squareup.com/oauth2/authorize?client_id=CLIENT_ID. The CLIENT_ID is the application identifier that you were provided when you registered your developer application in the Square developer portal. The developer portal is located at https://connect.squareup.com/apps. In the developer portal, you must also specify an oauth redirect_uri. After the user has authorized your application, they will be redirected to the redirect_uri that you specify with either an access token or a single use code (that can be exchanged by you for an access token.)
Also, please note that when you create an application in the developer portal, we provide you with a "personal access token" that you can use to perform API calls on behalf of your own user and test out the API.

How do you detect a redirect in an UIWebView iOS

I am using Xcode for a social media network site and I have a quick about the login/ signup process. For instance, I have a UIWebView that is linked to the login API page. (ex. www._.com/api_login.php?) When the user logs in with their credentials on that page the website redirects them to the success page (www._.com/api_success.php?) with the API authentication token that is stored in the iOS app for user specific tasks embedded in the website source code.
Here is my question: How can I tell Xcode to execute Javascript to grab the authentication token once it is on the (www._.com/api_success.php?) page? Keep in mind that the URL is specific to the user and has a user specific api key at the end of the link. (www._.com/api_success.php?apiconnectkey=123456789)
Thanks in advance for help with this,
Technology Guy
I'm not sure I follow 100%, but here are some things to look at:
Implement UIWebViewDelegate in your view controller and set the delegate property, then implement this message
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:
Docs
https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIWebViewDelegate/webView:shouldStartLoadWithRequest:navigationType:
This will let you see each URL that is being set on the web view. So, you could grab apiconnectkey.
Also, you can execute JavaScript on the page with
[self.webView stringByEvaluatingJavaScriptFromString:#"getToken()"]
where getToken() is a function on the page. This will return a string.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

Auto login in dropbox folder with OAuth

Only for internal use i need to display the content of our dropbox folder in a tableview inside our app without show for the login screen and handle back the authentication url.
In all the example i've found only the reference to
#import <DropboxSDK/DropboxSDK.h>
DBSession *dbSession = [[DBSession alloc]
initWithAppKey:#"INSERT_APP_KEY"
appSecret:#"INSERT_APP_SECRET"
root:INSERT_ACCESS_TYPE]; // either kDBRootAppFolder or kDBRootDropbox
[DBSession setSharedSession:dbSession];
and after tha the authentication is handle as url call back from the web page of dropbox.
but there is a way of handle the different part to the autenticantion and keep them in code?
May be could be easily handle with direct api call ?
In the dropbox "app console" i've found a "Generated access token" but i'm not understand how to use.
Thanks for your help and for your time.
The Dropbox API was designed with the intention that each user would link their own Dropbox account, in order to interact with their own files. However, it is technically possible to connect to just one account. The SDKs don't offer explicit support for it and we don't recommend doing so, for various technical and security reasons.
However if you did want to go this route, instead of kicking off the authorization flow, you would manually use an existing access token for your app. (Just be careful not to revoke it, e.g. via https://www.dropbox.com/account/security .) In the iOS Core SDK you'd need to use:
- (void)updateAccessToken:(NSString *)token accessTokenSecret:(NSString *)secret forUserId:(NSString *)userId;
Again though, this isn't a good idea. Since this would be a client-side app, any malicious user of your app could extract the access token and use it to bypass any access restrictions your app attempted to enforce. For example, they could access content they shouldn't or add or replace content with a malicious payload that other users would access.
Unless Dropbox changed their API recently, the user must manually supply a username / password. This can be done by popping out to the Dropbox app or using a Dropbox web view. You can not programmatically supply a default username / password.

Use Google Drive API to view files in iOS

I am currently working on a project wherein one of the requirements is to view a users Google Drive files on an iOS device. The SDK is all setup with oAuth 2.0 authentication and appropriate scopes. I've already pulled Google Plus, Calendar, and Drive data, but viewing the actual Drive files is proving to be harder than expected.
Currently i've got a table view with a list of all your Google Drive files, the idea is that when you click on them it should load the file.
Right now i've got a "DriveFileViewController", in it i've written:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.requestURL]];
[self.webView loadRequest:request];
[self.view setNeedsDisplay];
The self.requestURL in that case is the defaultOpenWithLink that Google lists in their API explorer at http://developers.google.com/apis-explorer/#p/drive/v2/drive.files.get.
The problem then is that the webView loads the requested URL but Google is asking for users to sign in again in the webView rather than showing them the file.
On my Mac, on the browser if you're authenticated and you click on the link from the API explorer it will work fine.
Im assuming their is some kind of cookie or header field that I need to set in my URL Request to let Google know the user is authenticated already and that they should be able to view their file.
Any help would be greatly appreciated.
You can use the access token as an Authorization header when loading an embedLink into a WebView.
Authorization: Bearer <access_token>
It will authorize the document with the same user authorized and authenticated on the app.

OAuth 2.0 integration in iOS

I have been trying for ages now to get OAuth 2.0 integration in my iPhone application.
I have searched and searched for libraries, tutorials, etc... But they have all lead me to a dead end. The main problem I have is that they either have deprecated code, or they just don't work or they have some documentation but its really hard to follow (for me anyway...).
The best OAuth2 library I could find for Xcode is this one: https://github.com/nxtbgthng/OAuth2Client
But the main problem with that one is it doesn't seem to do anything... I have followed all the documentation and instructions that came with it, but after building and running, it doesn't seem to authenticate....
So I guess my main question is: does anyone know of any good and up to date OAuth 2.0 tutorials for Xcode or any libraries for such a thing?
Because I am really struggling at the moment.... :(
Thanks for your time, Dan.
UPDATE 2: Here is my code (App Id and secret removed for security):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
oauthClient = [[LROAuth2Client alloc]
initWithClientID:#"MY_CLIENT_ID"
secret:#"MY_APP_SECRET"
redirectURL:[NSURL URLWithString:#"app://instagram-callback/?code="]];
oauthClient.delegate = self;
oauthClient.userURL = [NSURL URLWithString:#"https://api.instagram.com/oauth/authorize/?client_id=ab6dc96859bf43b3a488199ec72d9964&redirect_uri=app://instagram-callback/?code=&response_type=code"];
oauthClient.tokenURL = [NSURL URLWithString:#"https://api.instagram.com/oauth/access_token/"];
[oauthClient authorizeUsingWebView:myWebView];
}
- (void)oauthClientDidReceiveAccessToken:(LROAuth2Client *)client;
{
LROAuth2AccessToken *token = client.accessToken;
[NSKeyedArchiver archiveRootObject:token toFile:#"Path/To/MyAccessToken"];
}
- (void)checkAccessTokenForExpiry:(LROAuth2AccessToken *)accessToken;
{
if ([accessToken hasExpired]) {
[oauthClient refreshAccessToken:accessToken];
}
}
- (void)oauthClientDidRefreshAccessToken:(LROAuth2Client *)client;
{
LROAuth2AccessToken *token = client.accessToken;
[NSKeyedArchiver archiveRootObject:token toFile:#"Path/To/MyAccessToken"];
}
In almost all projects I have used AFNetworking because it's very powerful
-why re-invent the wheel every time :)
Furthermore, it also has an OAuth2Manager which is quite easy to implement and works rock-solid.
In Instagram documentation says that there are two ways to authenticate. One Explicit (for server-side auth) and one Implicit, for auth in a client (without server).
You are using the Explicit one inside the app, try changing the userURL to https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token and the tokenURL to http://your-redirect-uri#access_token=ACCESS-TOKEN.
You most likely cannot use a client secret in an iPhone App (because the client secret cannot be protected), you will need to authenticate through the services mobile app or mobile web site and then handle the redirect url, according to this link I found on the Uber api developer site:
http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
There is some code on this page to do just that, which I am testing now:
https://medium.com/swift-programming/learn-nsurlsession-using-swift-ebd80205f87c
There is also some good information on the bottom part of this page: http://www.idmworks.com/blog/entry/getting-started-with-oauth2client-on-ios
I recently made simple pod https://github.com/kohtenko/KOSocialOAuth.
You can easily connect Instagram, VK or LinkedIn. Feel free to send Pull Request with any other OAuth endpoint.
Check out this for using the Instagram API: https://github.com/shyambhat/InstagramKit. In the comments I see that you're having trouble with the redirect - look into Xcode's Redirect URI's for help with that: What's a redirect URI? how does it apply to iOS app for OAuth2.0?

Resources