I am new to iOS development & learning Swift 3 currently. However I'm stuck in a task to upload video to my youtube channel via YouTube DATA API insert.
As per Google instruction, we need to first get Access Token from Auth 2.0 by declaring the scope first
https://www.googleapis.com/auth/youtube.upload
I'm unable to get the Access Token using my Client ID which I have created on Google Developers Console. Also I don't find any Swift library available on Google which could have helped me easily.
Here is my tried sample code
func uploadVideo(accessToken) {
Alamofire.request("https://www.googleapis.com/upload/youtube/v3/videos", method: .post, parameters: ["part":"snippet", "autoLevels":true], encoding: URLEncoding.default, headers: nil, "access_token": "\(accessToken)").responseJSON { (response) in
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
}
I will be really thankful to you
Related
I want my app users to login with their Wordpress credentials. I've installed Alamofire & SwiftyJSON in order to do this, and I'm able to succesfully GET Wordpress endpoints to my app (via the Wordpress REST API). That said, I'm not sure how to authenticate my user on login?
I'm trying out the below, but the response is simply nil. Am I posting the user parameters to the wrong endpoint? Help is appreciated! I'm stumped.
let parameters: Parameters = [
"username": userField.text!,
"password": passField.text!,
]
AF.request("http://myurl.com/wp-json/wp/v2/users", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
switch response.result {
case .success(let value):
if let json = value as? [String: Any] {
print(json["Result"] as? Int)
}
case .failure(let error):
print(error)
}
}
I accomplished this some time ago for WooCommerce. I imagine it would be similar to WordPress. I found two ways of doing this.
One way you would need a JWT. I use the free plugin "JWT Auth" by Useful Team. Using the username and password you obtain the token and sign in the user.
The other way is using HTTPS Authentication using Google Cloud Functions. I opened the WooCommerce login page using a WKWebView and added the Cloud Functions addresses as return and callback URLs.
I know the answer is vague but hopefully it will provide you with different ways to approach the task. Explaining the entire thing would be almost like a tutorial.
I am currently developing an application for iOS. Most of the features that I wanted implemented I have already finished, but there is one feature in particular that I really need to have - Network Errors handling.
So for example: A user is trying to refresh his data inside my application. Instead of my app crashing or simply not doing anything, I would love for that exception to be caught, identified and then display a corresponding message on screen using AlertDialogs. for example:
Network Error - title;
Unreachable host, please check your network connectivity and try again - Message;
OK - button;
I was able to have this working in my Android application and it's quite useful, however, I am quite new to Swift and iOS development, so, please help me out here and point me in the right direction.
I am currently using latest Alamofire for sending HTTP Requests, here is my example of HTTP Request that I have implemented inside my application.
func loadProfile() {
let url = Constants.profileURL
let headers: HTTPHeaders = ["Cookie": "username; password"]
AF.request(url, method: .post, headers: headers).response {response in
if let data = response.data, let dataString = String(data: data, encoding: .utf8) {
if dataString.contains(Constants.loginSuccess) {
//Do something
}
}
}
}
Alamofire's DataResponse (and all response types) contain the Result of serialization. You can use that value to check whether serialization, and the request in general, succeeded or failed. You can then pass the result value to completion handlers to be dealt with as you wish.
I have this IBAction that is supposed to simply test sending SMS:
let todosEndpoint: String = "https://api.twilio.com/2010-04-01/Accounts/\(ACCOUNT_SID)/Messages.json"
let message = ["To": "+1555...5555", "From": "+555...5555", "Body": "Hello!"]
for item in message {
print(item)
}
Alamofire.request(todosEndpoint, method: .post, parameters: message, encoding: JSONEncoding.default)
.authenticate(user: ACCOUNT_SID, password: ACCESS_TOKEN)
.responseJSON { response in
debugPrint(response)
}
However, I'm getting this as a response:
[Result]: SUCCESS: {
code = 21603;
message = "A 'From' phone number is required.";
"more_info" = "https://www.twilio.com/docs/errors/21603";
status = 400;
}
I am sure I am providing the right phone number and authentication and I've used python to test sending messages using my API key and phone number.
Is the structure of my dictionary wrong or something?
Thank you for any help!
Twilio developer evangelist here.
We do not recommend that you make API calls directly to Twilio from your application. You would need to include your account credentials in the application somehow, so a malicious attacker could decompile the app, extract your details and abuse your Twilio account.
Instead, we recommend you set up a server of your own that can keep the credentials safe and then make requests to that yourself. Check out this blog post on sending SMS messages on iOS with Twilio, Swift and Alamofire.
I have a google iD of some user and I want to display his google profile photo in my app. In my swift code I try to fetch the json with necessary information (based on this answer https://stackoverflow.com/a/22526130/3766930 ). This is my code:
Alamofire.request(.GET, "https://www.googleapis.com/plus/v1/people/10263...4252?key=AI...Oo")
.responseJSON { response in
print(response)
switch response.result {
case .Success:
dispatch_async(dispatch_get_main_queue(),{
if let jsonData = response.result.value as? [[String: AnyObject]] {
print(jsonData)
}
})
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
but as a response I'm getting this error:
error = {
code = 403;
errors = (
{
domain = usageLimits;
extendedHelp = "https://console.developers.google.com";
message = "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.";
reason = ipRefererBlocked;
}
);
message = "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.";
};
I went carefully through my google developers console and I don't have any IP restrictions set up. This is how it looks so far:
I'm using the api key from here:
as you can see there is no domain limitation here:
What might be the reason of this weird error?
Remove the iOS bundle identifier restrictions for the API key in Google Developers Console. The bundle identifier restrictions only work when you use the Google client libraries.
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 .