OAuth2 with Yahoo API - ios

I want to read and write to a user's Yahoo Fantasy Sports data.
I'm trying to gain access via the Yahoo API using OAuthSwift.
My OAuthSwift object looks like this:
let oauth2Swift = OAuth2Swift(
consumerKey: "*****",
consumerSecret: "*****",
authorizeUrl: "https://api.login.yahoo.com/oauth2/request_auth",
accessTokenUrl: "https://api.login.yahoo.com/oauth2/get_token",
responseType: "code"
)
And I execute it like this:
self.oauth2Swift.authorize(withCallbackURL: "com.me.myapp", scope: "fspt-w", state: "", success: { (cred, response, params) in
print("✅ success")
}) { (error) in
print("🔴 error")
}
I've created an instance of the app on Yahoo's Developer Website and have made sure the callback domain matches the one used in the previous code snippet:
My iOS app successfully brings up the Yahoo login experience in the browser, however once the user logs in it shows the following error:
Developers: Please check the redirect URI in your request and submit
again.
So my question is: why I am getting the above error when attempting to authorize a Yahoo user with OAuth in Swift?

Identity Provider will redirect to client application with the URL specified in the callback. Try changing the callback Url to your application's auth endpoint. I used to test with callbackURL: "http://127.0.0.1:xxxx/auth/yahoo/callback" and used to work. And offcourse the configured callback domain should match with the domain in the URL

Related

AWS Amplify iOS SDK : FederatedSignIn Failed to retrieve authorization token on Amplify.API.post

I've been working with the Amplify SDK to get federatedSignIn working with my iOS app with "Sign in with Apple" and Cognito to eventually make calls to API Gateway / Lambda functions.
TL;DR : My access token does not appear to be "automatically included in outbound requests" to my API as per the last paragraph of this section of the docs : Cognito User pool authorization
I have successfully authenticated using the tutorial found here Authentication Getting Started and other various Youtube videos on the Amazon Web Services channel.
Upon successful sign in through Apple I'm given an ASAuthorizationAppleIDCredential object. This contains the user field (token) which I pass to the Amplify.Auth class using the following Swift code :
func signIn (with userId: String)
{
guard
let plugin = try? Amplify.Auth.getPlugin(for: AWSCognitoAuthPlugin().key),
let authPlugin = plugin as? AWSCognitoAuthPlugin,
case .awsMobileClient (let client) = authPlugin.getEscapeHatch()
else
{
return
}
client.federatedSignIn(providerName: AuthProvider.signInWithApple.rawValue, token: userId) { (state, error) in
if let unwrappedError = error
{
print (unwrappedError)
}
else if let unwrappedState = state
{
print ("Successful federated sign in:", unwrappedState)
}
}
}
All appears to be successful and to double check I use the following bit of code to ensure I'm authorized :
func getCredentialsState (for userId:String)
{
let provider = ASAuthorizationAppleIDProvider()
provider.getCredentialState(forUserID: userId) { (credentialsState, error) in
if let unwrappedError = error
{
print (unwrappedError)
}
switch credentialsState
{
case .authorized:
print ("User Authorized")
case .notFound, .revoked:
print ("User Unauthenticated")
case .transferred:
print ("User Needs Transfer")
#unknown default:
print ("User Handle new use cases")
}
}
}
In the console I see "User Authorized" so everything appears to be working well.
However when I then go to make a call to Amplify.API.post I get the following error:
[Amplify] AWSMobileClient Event listener - signedOutFederatedTokensInvalid
Failed APIError: Failed to retrieve authorization token.
Caused by:
AuthError: Session expired could not fetch cognito tokens
Recovery suggestion: Invoke Auth.signIn to re-authenticate the user
My function for doing the POST is as follows :
func postTest ()
{
let message = #"{'message": "my Test"}"#
let request = RESTRequest (path: "/test", body: message.data(using: .utf8))
Amplify.API.post (request:request)
{
result in switch result
{
case .success(let data):
let str = String (decoding: data, as: UTF8.self)
print ("Success \(str)")
case .failure(let apiError):
print ("Failed", apiError)
}
}
}`
I then went into the API Gateway UI and changed the generated Method Request on my resource from AWS IAM to my Cognito User Pool Authorizer thinking this was the issue. I also changed the awsAPIPlugin authorizationType to "AMAZON_COGNITO_USER_POOLS" in my amplifyconfiguration.json file. This unfortunately did not have any affect.
I've seen posts such as this issue User is not created in Cognito User pool for users logging in with Google federated login #1937 where people discuss the problem of having to to use a web ui to bring up the social sign in. I understand that Apple will reject your app sometimes for this. Therefore this is not a solution.
I then found this post which seems to resolve the issue however this appears to use the old version of the SDK? Get JWT Token using federatedSignIn #1276
I'm not great with Swift (I'm still an Objective C expert, but am slowly learning Swift) so I'm uncertain which path to go here and whether this is actually a solution? It does seem to be quite more complicated than the function I have that does my POST? The RESTRequest does seem to be a simple and easy solution but I'm uncertain how to pass it the Authorization token (or even how to get the token if it is needed here).
However, everything I've read about the SDK is that the authorization should be handled automatically in the background according the docs in my first link above. Specifically pointed out, again, here : Cognito User pool authorization. The last paragraph here states 👍
With this configuration, your access token will automatically be included in outbound requests to your API, as an Authorization header.
Therefore, what am I missing here as this does not appear to automatically include my access token to my outbound requests to my API?

Consuming getstream.io in iOS app but documentation is not enough

I just want to implement only get stuff from getstream.io in iOS application
Client.config = .init(apiKey: "<KEY>", appId: "<ID>", token: "<TOKEN>")
after writing this line of code
let timeline = Client.shared.flatFeed(feedSlug: "timeline", userId: "34")
timeline.get { (response) in
log(response)
}
I am getting 403
Basically I want to get timeline from getstream but I am not able to understand what is token in this case?
you need to generate your auth token serverside and pass that back to the iOS client.

Firebase: How does it know what account my OAuth token is associated with?

I'm about to build an iPad app that uses OAuth 2.0 using this cocoapod: https://github.com/OAuthSwift/OAuthSwift. I'm looking through this firebase tutorial (https://firebase.google.com/docs/auth/ios/custom-auth) but, I'm a little confused.
Given I have this code:
oauthswift = OAuth2Swift(
consumerKey: "********",
consumerSecret: "********",
authorizeUrl: "https://api.instagram.com/oauth/authorize",
responseType: "token"
)
let handle = oauthswift.authorize(
withCallbackURL: URL(string: "oauth-swift://oauth-callback/instagram")!,
scope: "likes+comments", state:"INSTAGRAM",
success: { credential, response, parameters in
print(credential.oauth_token)
FIRAuth.auth()?.signIn(withCustomToken: credential.oauth_token ?? "") { (user, error) in
// ...
}
},
failure: { error in
print(error.localizedDescription)
}
)
How would firebase know what account that token is associated with if the token is random and different for each device? I don't know how to associate that token to an account. Like if I signed in to instagram with username cool#example.com and password blah123 and the oauth_token was 123abc how would I know that 123abc was connected to cool#example.com in my firebase account if 123abc was random?
I feel like I'm misunderstanding one of the concepts.
You need to create the custom token from the Instagram access token server-side using the Firebase SDK's. You then need to request this from the server via the client and signIn withCustomToken: using the token in the response.

Facebook OAuthSwift redirect_uri not supported iOS

I am trying to use OAuthSwift to get the user's access token in my app. I already did this with Twitter and Instagram, but Facebook is not helping me.
The code I am using is the following:
func doOAuthFacebook () {
let oauthswift = OAuth2Swift(
consumerKey: "####",
consumerSecret: "####",
authorizeUrl: "https://www.facebook.com/dialog/oauth",
responseType: "token"
)
oauthswift.authorizeWithCallbackURL(NSURL(string: "myApp://myApp/facebook")!, scope: "publish_stream,email", state: "FACEBOOK", success: { (credential, response, parameters) -> Void in
self.showAlertView("Facebook", message: "oauth_token:\(credential.oauth_token)")
}) { (error) -> Void in
print (error.localizedDescription)
}
}
The callbackURL that I am using works for the other two social networks. From Facebook I am receiving this message when Safari tries to open the page:
The redirect_uri URL is not supported
I don't know if I have to do something to my app in Facebook Developers page, or it is something else.
This case is documented in project wiki page
https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL
two way:
have an http server and redirect to your application scheme
use a delegate into a webview to handle fake http url and redirect to your application scheme or call directly
OAuthSwift.handleOpenURL

Facebook get number of likes on custom object without login

I am using og.likes to like custom object from my app, with the help of Facebook-iOS sdks. I am able to like object and also read the like.
Now I want to know if there is any way in graph API that, I can show total number of likes on an object to a user who is not logged in to our app via Facebook ? i.e without access token
As per FB developer docs:-
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/likes
It is mentioned that you need the same permissions required to view the parent object are required to view likes on that object, means an user access token with read_stream permission is required.
NOTE:
But if a user want to get the no of likes for Facebook page or profile or object , then it can be easily done by this:-
https://graph.facebook.com/< your object id>/
Then you will receive json response with Like Count:-
{
"id": "12345",
"link": "http://www.facebook.com/pages/MY iOS Page/102445859460201",
"likes": 150,
"type": "page"
}
Code:-
let url = NSURL(string: "http://graph.facebook.com/"+ObjectID)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
// Fetch the data
var jsonDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
let likesCount = jsonDictionary["likes"] as? String
}
Got Response from Facebook Team :
Since Graph API v2.0, all requests to the API require an access token.
You can proxy this request through a web-server and use your app
access token for example.
You could as well make the request from your app by shipping your app
access token with the app, but I highly advise against this as the app
token should be kept secret.
Please note that the SDK does not support using the app access token
for security reasons. So you have to write your own networking code to
fetch this data.
I got the solution from
How to get Likes Count when searching Facebook Graph API with search=xxx
Here in this link it's not the exact solution but i have tried my way and unfortunately it worked for me, and i am quite satisfied with it, still using it in my application.
https://graph.facebook.com/v2.1/{Object-id}?fields=likes.summary(true)
You will need an access token, no need to login any user, you can generate an APP Access token to get that info .
jQuery.get("https://graph.facebook.com/oauth/access_token", {
'grant_type': "client_credentials",
'client_id': "000000000000000",
'client_secret': "0000000000000000000000000000000000"
}, function (data) {
// data is the app access token you will need for calling graph api
});
Thats more complicated but solves the necessity to login the user to get public informations from the graph .

Resources