cloudkit JS API authentication - cloudkit

Ok, I have a working cloudkit container that I am currently using as a database for my app. Now I want to try using the cloudkit API service. In the API access section, I just add a new token. this is the overview of my simple set up:
I tried using Postman to make one POST call, trying to see if my structure of my API call is right. my cloudkit container name is iCloudkit.izzuddin.CoffeeApp, and the records name is Cafe, so this is how I create the api call:
cloudkit.com/database/1/iCloud.izzuddin.CoffeeApp.Cafe/-development/public/records/modify?NewToken=031c663b3314f16ff3ee04719894b6f4e17132ad315eae27fa3e4374fd0b4e81
this is the returned value:
{
"uuid": "737d61ec-a668-4386-9512-b2ec05a8a394",
"serverErrorCode": "AUTHENTICATION_FAILED",
"reason": "no auth method found"
}
Can anybody who have experience explain how to structure the api call? thanks in advance.

Try:
https://api.apple-cloudkit.com/database/1/iCloud.izzuddin.CoffeeApp.Cafe/development/public/records/modify?ckAPIToken=031c663b3314f16ff3ee04719894b6f4e17132ad315eae27fa3e4374fd0b4e81
This should return 421 response AUTHENTICATION_REQUIRED with a redirectURL which you can use for signing in.
The next steps depend on what kind of app (web app / server to server / native) you want to build.

CloudKit Web Service needs not only your API token, but also needs [Web Auth Token], which represents the users.

Related

How to get Moodle user id through Web Services API using token

Some background
I am connecting external application (server) with Moodle through Web Services API.
First a user obtains web services token through a call to /login/token.php.
Above token can be used to call web services functions through /webservice/rest/server.php endpoint.
The problem
In some cases I need to provide userid, which I don't know, because /login/token.php does not return it.
Things I've tried
I searched through the Web Services api documentation and found core_session_time_remaining function. Documentation says it returns userid and timeremaining properties:
object {
userid int //The current user id.
timeremaining int //The number of seconds remaining in this session.
}
The problem is, returned userid is always 0, irrelevant of what token was used to call this function. Is it a bug in Moodle, or am I using it wrong? How can I get userid?
did you ever tried using the core_webservice_get_site_info wsfunction?
I use it and works fine.
According to the docs: "Return some site info / user info / list web service functions" API Doc

Retriving data from facebook events

I have to create an webpage where i need to show all the events from the past or upcoming ones from an facebook page. I have to retrive the title, description, image, time and place of every single one and make a box with it.
After that, i have to retrive all the interested people.
I'm a beginner trying to make big things! Thank you very much.
This isnt a simple problem - I have just done this so will give you a start.
You need to use Facebooks Graph API to get the data, you will need to read up on the Graph API and how to use it. Documentation can be found here. Its worth noting due to a security breach the API isn't offering all of its services to new developers currently but you can get most - read about this here.
You can query the API using any language but I suggest do it with python and use the requests module. Alternatively you could use this SDK which makes things a lot easier, trust me! With the SDK you need to get a user access token which you can get from here.
SDK
With the SDK its possible to do this
import facebook
graph = facebook.GraphAPI(access_token="your_token", version="2.12")
event = graph.get_object(id='event_id',
fields='attending_count,declined_count')
print(event['attending_count'])
print(event['declined_count'])
You can add more fields to the fields list. However you can use any language you choose to request the data, as you can just construct your own URL's.
Graph API & Requests
This is how I did it in python using requests to query the API.
import requests, json
#Make a requests session to fetch data from URL's
session = requests.Session()
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=10))
#API base URL
baseUrl = 'https://graph.facebook.com/v2.11/'
#Fields you want to get
fields = ['id', 'name', 'start_time', 'end_time', 'description', 'place']
#replace CLIENT_ID and CLIENT_SECRET with ones from a facebook app (make one)
tokenPath= 'oauth/access_token?client_id={0}&client_secret={1}&&'\
'grant_type=client_credentials'.format(CLIENT_ID, CLIENT_SECRET)
#Get the generated token
token = session.get(baseUrl + tokenPath).json()['access_token']
event = session.get(baseUrl, params={
"ids": "event_id",
"fields": ",".join(fields),
"access_token": token,
}
).json()
print(event["name"])
print(event["place"])
You can make a facebook app here which is where the CLIENT_ID & CLIENT_SECRET come from.
Why would you use URL requests and not the SDK?
Using the SDK the generated user access tokens expire every day, its possible to extend them but only for 60 days. If you want to use the SDK it has a method built in which allows you to extend the token, in order to extend the token you also need to create a facebook app. Its also possible to extend tokens using this website, though I cant vouch for its security - it could potentially access and exploit your data.
To extend the token with SDK:
import facebook
graph = facebook.GraphAPI(user_access_short_lived_token)
extended_token = graph.extend_access_token(app_id, app_secret)
print(extended_token)
Using the URL approach, you create a client access token at runtime (read about user access token vs client access token here) and the token is valid so long as your app is active. So if you are going to use this a lot or want to create it and not have to change/extend tokens then use the URL approach.
Hope this gets you started.

WPF talking to secured web api using AutoRest

I just can't seem to get anywhere with this.
I have a web api running fine, with a wpf application using the api via AutoRest.
The api has been uploaded to Azure (App Services)
I now want to lock down the api, so users log in via Active Directory. Again, all users are currently there.
RestCredentials = new TokenCredentials(tokenAuthResult.AccessToken);
Using the RestCredentials I pass in the credentials as type
ServiceClientCredentials
using (var db = new BuxtedAPI(Model.Helpers.Credentials.RestCredentials))
{
var res = db.GetComboList();
ComboValueList = new ObservableCollection<ComboValue>(res);
return ComboValueList;
}
I can see on the log in Azure that the user successfully logs in.
But the system just dies with
Operation returned an invalid status code 'InternalServerError'
No more feedback at all.
Just to note, I am using swagger also here.
I am assuming that the BuxtedAPI call that passing the credentials should pass through the bearer token for the api call to authorise.
I have set nothing else on on webapi code base, no [AUTHORIZED] or anything.
Any chance anyone can help me along here.
Thanks Scott
If anyone else gets in this situation.
The process required Resource Id to be the web app and the clientid to be the native app id.
Scott

How do I get the campaigns list through the ads API on Facebook?

I am trying to get the campaigns list from the Graph API Explorer. I'm using the request found at https://developers.facebook.com/docs/marketing-api/reference/ad-account/campaigns/.
GET /v2.8/{ad-account-id}/campaigns HTTP/1.1
Host: graph.facebook.com
I'm' business manager and admin of the page used for ads. I found the AD_ACCOUNT_IDs using the following request on the Graph API Explorer.
GET /v2.8/me/businesses HTTP/1.1
Host: graph.facebook.com
I choosed the one which had the higher privileges.
The error reported when submitting the first request for campaigns is:
{
"error": {
"message": "Unsupported get request. Object with ID '1015359838XXXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
"type": "GraphMethodException",
"code": 100,
"fbtrace_id": "DLyfwGbM8fi"
}
}
What's wrong?
Thanks in advance,
Mattia
#Ryan's answer saved my day. It may sound stupid/silly but I was doing this mistake and I was just stuck at it.
Going through the FB docs for using the Marketing Apis.
I was trying to build the custom audience, this is how the curl request looks like -
One of the basic things to keep in mind while using the Marketing API is, almost all the APIs ask for these two things -
1) ACCESS_TOKEN
2) AD_ACCOUNT_ID
For people who are just starting with the FB Marketing API or somehow stuck in finding these values, I will just tell you how to get these values, so that you don't have to waste your time as I did.
So for getting the ACCESS_TOKEN, go to your app dashboard then click Add Product and then select Marketing API. Once you added the product, this is how the screen should look like -
Now just check the permissions and click on the Get Token button, a token would be generated. Just copy the token in some file and save it.
Now for getting the AD_ACCOUNT_ID value, just go to the Adverts Manager page -
The number that is written inside the red box in you Adverts Manager page is your AD_ACCOUNT_ID.
The final step would be to go back to you app dashboard again, Settings > Advanced
Click on Ads API and then enter the AD_ACCOUNT_ID here. Thats it, you have finished the Access and Authentication process for using the marketing API via your App.
Now comes the part where I was doing the silly mistake. The curls request looks like this -
curl \
-F 'name=My new CA' \
-F 'subtype=CUSTOM' \
-F 'description=People who bought from my website' \
-F 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.8/act_<AD_ACCOUNT_ID>/customaudiences
First I replaced ACCESS_TOKEN with its value.
Then instead of replacing only the <AD_ACCOUNT_ID> I replaced the whole string act_<AD_ACCOUNT_ID>, with the AD_ACCOUNT_ID value.
So I was getting this error while making request to the API which was -
{"error":{"message":"Unsupported post request. Object with ID '120574219' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https:\/\/developers.facebook.com\/docs\/graph-api","type":"GraphMethodException","code":100,"fbtrace_id":"YsSvKKwgLMQ"}}
I don't know why I made this mistake but I am sure this is one of the common mistake others are doing too. Since the response from FB was misleading, I wasted a lot of my time in understanding and reading on the API permissions and authentications.
Hope this article was helpful and save your time.
I similarly was having problems trying to retrieve a list of all campaign IDs within an ad account.
I already am able to retrieve campaign data by ID, so I obviously have permissions already.
But the Facebook error message is misleading.
When retrieving an ad account (in order to then display its campaigns or whatever you want within it), you need to retrieve by its ID prepended with 'act_'.
E.g you could request /act_123456789000001234/campaigns instead of /123456789000001234/campaigns.
Thanks so much to #Jan Sommer at https://stackoverflow.com/a/39974857/470749.

Google Cloud Storage - Trying to get an Access Token in Elixir

I'm trying to use OAuth2 for Elixir to get an access token to interact with the Google Cloud Storage API. I've been experimenting with the OAuth2 Playground and I can make the requests and they give me back the token. Upon doing a basic GET request (to list the buckets in the project) it all works correctly, but of course I need to get the token automatically inside my app instead of copy pasting.
def token do
client = OAuth2.Client.new([
strategy: OAuth2.Strategy.AuthCode, #default
client_id: "myClientID",
client_secret: "myClientSecret",
site: "https://www.googleapis.com",
redirect_uri: "https%3A%2F%2Fdevelopers.google.com%2Foauthplayground" #I HAVE NO IDEA WHAT SHOULD BE HERE???
])
token =
client
|> OAuth2.Client.put_param(:code, "myAuthCode")
|> OAuth2.Client.get_token!()
I keep getting a "Not Found" message.
I'm quite confused by this, if someone could help I'd be most thankful.
EDIT:
Apparently the request should be something like this:
https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id=myClientID&nonce=someRandomStringIthink&scope=https://www.googleapis.com/auth/devstorage.full_control&redirect_uri=WHEREDOIGETTHIS
I still have no idea where to find the Redirect URI. The documentation says it should be in the developer's console.
You can check an example for Google Auth here. It covers everything you need to interact with gcs.