Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ipeirotis committed May 1, 2019
1 parent ddec90a commit e0eb833
Show file tree
Hide file tree
Showing 2 changed files with 885 additions and 0 deletions.
226 changes: 226 additions & 0 deletions 04-WebAPIs/D0-Twitter_Signup.ipynb
@@ -0,0 +1,226 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Register-a-new-application\" data-toc-modified-id=\"Register-a-new-application-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Register a new application</a></span></li><li><span><a href=\"#Getting-access-to-Twitter-APIs\" data-toc-modified-id=\"Getting-access-to-Twitter-APIs-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Getting access to Twitter APIs</a></span></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Gaining access to the Twitter API in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://raw.githubusercontent.com/khof312/Summer2018_ProfHoffmannPham/master/04-WebAPIs/images/Python_Twitter.jpg\" width=\"200\">\n",
"\n",
"The guide below shows the steps of registering an application that you can integrate with **Twitter**. Registering the application is the first step in developing an application that is integrated into its users' social graph."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Register a new application\n",
"\n",
"All **Twitter** users are potentially **Twitter** application developers. Simply visit https://apps.twitter.com/ and sign in with your **Twitter** credentials (if you don't have them, you'll have to sign up for a new Twitter account). \n",
"\n",
"Click the \"Create new app\" button near the top. A new page with the **_Create an application_** form requires basic information about your application.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/khof312/Summer2018_ProfHoffmannPham/master/04-WebAPIs/images/create_an_application_1.jpg\">\n",
"\n",
"- In the **_Name_** field name your application in 32 characters or fewer. \n",
"- In the **_Description_** field describe your application in 10 to 200 characters.\n",
"- In the **_Website_** field, you can give a pointer to your website. (It does not really matter, as we will not be using that functionality yet...just pick any site, e.g. your Github account or our course website).\n",
"- The **_Callback URL_** leave it empty for now.\n",
"\n",
"The **Developer Agreement** section outlines rules you must agree to follow if you build an application that uses **Twitter’s** API. If you agree to the rules, check \"Yes, I agree\".\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/khof312/Summer2018_ProfHoffmannPham/master/04-WebAPIs/images/create_an_application_2.jpg\">\n",
"\n",
"Click \"Create your **Twitter** application\" to complete the form and you will be redirected to the application settings page.\n",
"\n",
"In the opened window you’ll then be presented with lots of information. \n",
"\n",
"\n",
"The main field to note is **_Consumer key_**, which forms part of your application’s credentials for **Twitter**. Next to the key, click \"manage keys and access tokens.\" Now, you'll also see your **_Consumer secret_**. Together, these fields are needed to do almost anything with **Twitter**, including going through the OAuth authorization flow and working with **Twitter’s** REST API. \n",
"\n",
"But we’re not quite done yet. We now need to authorise the **Twitter** app for your **Twitter** account. To do this you should select \"Key and Access Tokens\" menu button and create your access token (at the bottom of the page). This **access token** will allow your **Twitter** application to read **Twitter** information. You’ll be able to get data of your tweets, mentions, lists and more. \n",
"\n",
"<img src=\"https://raw.githubusercontent.com/khof312/Summer2018_ProfHoffmannPham/master/04-WebAPIs/images/create_an_application_3.jpg\">\n",
"\n",
"As a result you will see the window with consumer and token keys for your application \n",
"\n",
"<img src=\"https://raw.githubusercontent.com/khof312/Summer2018_ProfHoffmannPham/master/04-WebAPIs/images/create_an_application_4.jpg\">\n",
"\n",
"Above the four fields are highlighted. You will need these long horrible strings of characters for your **Twitter** app. It goes without saying that you should keep these secret. If anyone was to get these keys, they could effectively access your **Twitter** account.\n",
"\n",
"However, if you want to do more advanced stuff like sending tweets or deleting, you’ll need to change your access type in the \"Permission\" menu window. Change the access type to \"Read and Write\" to be able to read **Twitter** data and send tweets and select \"Read, Write and access direct messages\" if you want to also have access to your direct messages."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## Getting access to Twitter APIs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In class, we'll explore API access. For now, let's just run the following code to test that you've connected:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!sudo -H pip3 install -U requests_oauthlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import requests\n",
"from requests_oauthlib import OAuth1\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<span style=\"color:red\"> Important: Paste **your** key, secrets, and tokens below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Let's define consumer and access keys and secrets for getting access to Twitter API through your application\n",
"consumer_key = 'PASTE_CONSUMER_KEY_HERE'\n",
"consumer_secret = 'PASTE_CONSUMER_SECRET_HERE'\n",
"access_token = 'PASTE_ACCESS_TOKEN_HERE'\n",
"access_secret = 'PASTE_ACCESS_SECRET_HERE'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# You will authenticate yourself using OAuth1 object\n",
"auth = OAuth1(consumer_key, consumer_secret, access_token, access_secret)\n",
"print(auth)\n",
"\n",
"print(auth.client)\n",
"\n",
"# If the authentication was successful, you should see the name of the account print(out)\n",
"url_0 = 'https://api.twitter.com/1.1/account/verify_credentials.json'\n",
"res = requests.get(url_0, auth=auth)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If everything worked, your name and followers will appear:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"My name is\", res.json()[\"name\"])\n",
"print(\"I have\", res.json()[\"followers_count\"], \"followers! Yeah!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 1
}

0 comments on commit e0eb833

Please sign in to comment.