gpt4 book ai didi

python - 获取 Instagram 访问 token

转载 作者:行者123 更新时间:2023-11-28 17:39:41 25 4
gpt4 key购买 nike

如何仅从 Python 获取访问 token ?

我的代码是这样的:

import bottle_session
import bottle
import cgi,os
from bottle import route, redirect, post, run, request
from instagram import client, subscriptions
import urllib
from pygeocoder import Geocoder

results = Geocoder.geocode("University of Santo Tomas,Manila,Philippines")
print(results[0].coordinates)

latitude=results[0].latitude
longitude=results[0].longitude

CONFIG = {
'client_id': '',
'client_secret': '',
'redirect_uri': 'localhost:8515/oauth_callback'
}

unauthenticated_api = client.InstagramAPI(**CONFIG)

url = unauthenticated_api.get_authorize_url(scope=["likes","comments"])

code = request.GET.get("code")
print "CODE:" + str(code)

access_token, user_info = unauthenticated_api.exchange_code_for_access_token(code)
print "access token= " + access_token
print access_token

api = client.InstagramAPI(access_token=access_token)
session['access_token']=access_token

最佳答案

这就是我在 Django 中做同样事情的方式:

SCOPE = ('basic', 'comments', 'relationships', 'likes')


def get_api(access_token=None):
if access_token is not None:
# authenticated api
return InstagramAPI(access_token=access_token)
else:
# unauthenticated api
return InstagramAPI(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URL)


def logout(request):
try:
del request.session['token']
del request.session['instagram_user']
except KeyError:
pass
return redirect(REDIRECT_URL)


def signin(request):
api = get_api()
redirect_uri = api.get_authorize_login_url(scope=SCOPE)
return HttpResponseRedirect(redirect_uri)

# Redirect url goes here
def instagram(request):
template = 'account/instagram.html'
access_token = request.session.get('token')
instagram_user = request.session.get('instagram_user')

if not access_token:
code = request.GET.get("code")

if not code:
t = loader.get_template(template)
c = RequestContext(request, {})
return HttpResponse(t.render(c))

unauthenticated_api = get_api()
access_token, instagram_user = (
unauthenticated_api.exchange_code_for_access_token(code))
request.session['token'] = access_token
request.session['instagram_user'] = instagram_user
if not access_token:
return HttpResponse('Could not get access token')

api = get_api(access_token)
instagram_user = api.user(instagram_user['id'])

t = loader.get_template(template)
c = RequestContext(request, {'insta_user': instagram_user})
return HttpResponse(t.render(c))

希望对你有帮助

关于python - 获取 Instagram 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26242011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com