programmatically query FB ads managers api for purchases - facebook

I am writing a PHP script to query some data from FB Ads Manager (business.facebook.com) and display it back to the user.
I need to determine the total # number of "website purchases" (clicked or viewed), using either cURL or facebook/php-ads-sdk.
I have an access token, have completed some basic queries in both cURL and API, so setup and authentication are verified. What I need is the proper API route or cURL to get the required info.
The cURL below works for a single ad, but I am working with 1000s of ads. If I don't find a better way, I may traverse the entire account and request data for each ad each time, but that seems very inefficient. Ideally, I'm looking for one API or cURL with ACCOUNT_ID, DATE_RANGE, and ATTRIBUTION_WINDOWS. (NOTE: I don't even need data per ad, just a total # for the supplied account/date/attribution).
//single ad cURL solution
curl -i -G \
-d 'fields=actions,action_values' \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.8/<AD_ID>/insights
A lot of FB's online docs for Ads/Insight are missing or deprecated, and what's there is sparse. FB support will not help with this at all. Any advice is greatly appreciated!!!

Related

Facebook api - request business manager partnership

Is it possible to programmatically request business partner with access assets business owns.
Facebook business-manager-api describes best practices to work with business-manager-api. Would be very handly if our app can request partnership to client or agencies business managers programmatically.
Update
So after digging documentation, playing with Facebook Graph API and asking on official Q/A groups we came to conclusion, that at this moment Facebook API doesn't provide any endpoint to request business partnership. So we refactor our flow and now we will request access to business's ad accounts. I will accept answer as correct, cause at this moment this is the only possible solution
The Business Manager API provide a functionality to request access to assets. This is documented in the Business-to-Business Functions of the Assets section.
For example,
Business Manager may request access to an ad account or a page owned by another business manager. They must specify the roles that they want need to be able to assign in the request.
To request AGENCY access, you must provide permitted_roles in your request. You can only send request to assets to business manager that you intend to approve and that they must already know your business.
For example, a business that needs access to adaccount_id and needs to be able to assign its employees as GENERAL_USER and REPORTS_ONLY would make this POST call:
curl \
-F "adaccount_id=act_<AD_ACCOUNT_ID>" \
-F "permitted_roles=['GENERAL_USER','REPORTS_ONLY']" \
"https://graph.facebook.com/<API_VERSION>/<BUSINESS_ID>/client_ad_accounts?access_token=<ACCESS_TOKEN>"
When using this api now
curl \
-F "adaccount_id=act_<AD_ACCOUNT_ID>" \
-F "permitted_roles=['GENERAL_USER','REPORTS_ONLY']" \
"https://graph.facebook.com/<API_VERSION>/<BUSINESS_ID>/client_ad_accounts?access_token=<ACCESS_TOKEN>"
it says
"(#3) Application does not have the capability to make this API call."
you should use Business On Behalf Of instead.

Get latest tweets for multiple screen_names through Twitter Rest API in CURL

I'm completely new to the Twitter Rest API. I have generated a curl command using OAuth tool in twitter to get the tweets posted by a single user.
curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json' --data 'count=2&screen_name=twitterapi' --header 'Authorization: OAuth oauth_consumer_key="AAAAAAAAAAAAAAAAAAAA", oauth_nonce="BBBBBBBBBBBBBBBBBBBBBBB", oauth_signature="CCCCCCCCCCCCCCCCCCCCCCCCCCC", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1471672391", oauth_token="DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD", oauth_version="1.0"'
Reference: https://dev.twitter.com/rest/reference/get/statuses/user_timeline
I got the data for a single screen_name and now I would like to pull the tweets for multiple screen_names at once through Curl.
I have tried this using POST but it didn't work. I have been trying since 7 days.
Can anyone help me to resolve this ?
Thanks in Advance.
Aswin
As far as I know this is not possible with one API call. The operation you are referencing only allows for the retrieval of statuses by a specific user, not multiple users. Also, it requires a GET request so a POST won't get you very much.
You can look at other operations to see if they fit your requirements. For example GET statuses/home_timeline gets the statuses of all users that are followed by the account with which you request this.
However if the users whose statuses you are looking to retrieve have nothing like this in common than the best way to go is to use separate requests for each user and use the API as intended.

GitHub API find all commits by user ID with timestamp

Is there an efficient way to find all recent commits by a specific user across all public repos?
I am currently use /events/public and filtering out those event.type === "PushEvent". However this is not very efficient because
The commits in the PushEvent does not have timestamp, which means I need additional requests to fetch their timestamps through commits[][url].
There is a limit of 60 requests/hour which gets quickly used up because I need to fetch timestamp of each commit.
Is there any better way to do this?
No unfortunately there is no better way to retrieve commits for the user.
However there is a workaround for the rate limit:
Documentation says
For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour.
You can generate an access token and use it as an OAuth token.
How to use token
If you're going to use Basic Authentication
you need to add new header
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
where the string after Basic is a Base64 encoded string of
your_user_name:your_token
If you're using curl
curl -u username:token https://api.github.com/user
or
curl https://username:token#api.github.com/

How can I create a HTTP/Rest request to upload something to Google Drive

I want to create a command line tool to download and upload files to my Google Drive. I do not want to use any language that Google already provides api calls (Java, php, javascript, C#, etc..).
I will use Haskell but that is not relevant with the question.
What I want to do now is form a POST request to upload a file. My request now is something like this:
POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: */*
Content-Length: 5
Authorization: Bearer AIzaSy......jH683-FRO9GI
Cache-Control: no-cache
test1
and I get this response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Next to Authorization: I put the API Key I created from Google Developer's console.
I figured I have to do more than one requests to make this work. First I have to get an auth code, then use that code to get an auth token and then use that token to my post request.
How can I make that happen? Can someone give me a short example?
There are several possible issues:
#1
You shouldn't be sending the API key with your requests.
#2
Your key might have expired. They only last for 60 minutes.
#3
Your access might have been revoked. (But in your case, if you are able to get an access token, then this is not the case)
#4
You did not include an access token in your request.
I think your problem is a result of #1 or #4, but I listed the other possibilities for reference.
Edit #1:
To address the comment, I will explain the process Google uses to authenticate requests. This question addresses some of the process but I am going to rewrite some of it in the context of this question. (The linked question was asked by me and then later answered by me)
There are two main items you need to make a request to a Google API:
refresh_token - Used to get more access_tokens and never expires
access_token - Used to send API calls (example: upload a file) and expires every 60 minutes.
In order to get either of these you have to follow certain steps.
Obtain a OAuth 2.0 client ID through the Google Developers Console
Obtain an access token from the Google Authorization Server
Send the access token to an API
Refresh the access token, if necessary
More specific information about this "general" process can be found here.
The tricky step is getting the refresh token because once you have that stored in a secure location, you can do anything with simple httpRequests.
To get a refresh token, a user has to agree to give your project access to certain features. This is called scopes in Google language. You can do this in many different ways (according to Google):
There are several ways to make this request, and they vary based on
the type of application you are building. For example, a JavaScript
application might request an access token using a browser redirect to
Google, while an application installed on a device that has no browser
uses web service requests.
The one thing they don't tell you is that, in your case, it doesn't matter what medium you use to get the refresh_token because you are only using one account. The Google Drive API isn't really designed to be used like a server. It is designed to be used by a developer that wants to store information on each of its user's accounts. Like if you had a picture app, you could have a feature that stores edited picture on someone's personal Google Drive account.
What you (and many others who have recently asked about) want to do is essentially use a Drive account as a server. That means that the medium through which you get your refresh_token does not have to be related to the medium in which you are using the Drive Account.
For example in my specific case, I wanted a way to store user pictures on a server for free for an android app. I am using this free service called Parse to act as my database server, but they give you very limited file storage in their free tier.
Because of this, I decided to try and figure out how to use Google drive accounts to expand my storage. I created a generic gmail account something like "hostingaccount#gmail.com" to be the host of the files (15g for free).
To get my refresh_token, I setup a php server to authenticate that one account. When someone goes to the page I setup, they are prompted to login and then grant access to my project to use their Google Drive account (Specific scope: https://www.googleapis.com/auth/drive). I then setup the script to print the refresh_token for that account on the screen. I copied that string an put it into my server when now I can easily send httpRequests to:
https://www.googleapis.com/oauth2/v3/token/ to get an access token and to:
https://www.googleapis.com/upload/drive/v2/files to upload files.
I link it at the top of this edit, but this answer shows you how to get a refresh token using my php method. I never spent the time to try an figure out how to get a refresh token any other way, but if you read my whole long answer I think I mention that I believe that this can also be done with the Android Google Drive API.
I have tried to help so many people with this problem, maybe I should just start a blog and make a tutorial about it ;)

OAuth 2.0 to Salesforce without a webpage

I am trying to connect to Salesforce.com using their OAuth 2.0 interface. I have found solutions like LROAuth2Client that allow me to open a webpage and authenticate like that. But is there a way to do this all through the backend without going to a webpage first so I can have it more integrated into the App?
I am new to OAuth so I don't know all the limitations.
Thanks in advance.
Salesforce supports the OAuth2 username/password option, which is all done without a webpage. e.g. using curl you'd do
curl -v https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=xxxxxxxxxx" -d "client_secret=1234567890" -d "username=noreply#salesforce.com" -d "password=XXXXXXXXX"
Having said that, the web/interactive flow is pretty straightforward to use from iOS, using a custom scheme URL for the callback (trying to use a library is possibly making it harder than it actually is). The web based flow is important for users that are configured for alternative authentication flows (e.g. SAML based SSO), which won't work over the username/password flow.
There should be a video online somewhere of the REST & OAuth session from dreamforce'10, where the security PM wrote an iPhone oAuth client based app on stage.
It depends on your goal. One reason for using oauth2 is you want better insight into what is is running. Lets say for example you have many cron API applications all connecting to the same site. In that case, since they are cron applications, you don't even necessarily have a web browser running on your cron servers. You could of course just configure username and password, but then if you have a password reset you have to update all your cron servers, and you don't even necessarily know how many servers that is. Especially if they are coming through a NAT.
In this type of scenario, a very workable solution is to use a visual force page on salesforce as the callback URL. The administrator can login salesforce and then use the visual force page to generate a refresh token, they then hand off for use with the cron job.
You know have oauth 2 in your auditing stage. The token you have handed out, can be restricted to API. And hopefully if salesforce has don't oauth 2 correctly, you can reset the password on the user login as often as need be, without effecting the tokens you have handed out.
Bill
There is one more approach to this. You can visit my site for the approach.