2015-05-08 11 views
5

मैं ऑफ़लाइन पहुंच के लिए सही ढंग से स्कॉइंग कर रहा हूं और इसे संग्रहीत कर रहा हूं। हर 60 मिनट, जब आवश्यक हो, मैं एक नया access_token पुनर्प्राप्त। कोड नहीं बदला है, लेकिन अजीब बात यह है कि जब वह पहले प्राधिकरण के माध्यम से चला गया।पायथन में Google Plus में अमान्य अनुदान देकर टोकन और रीफ्रेश टोकन एक्सेस करें?

client_id  ="xxxxx.apps.googleusercontent.com" 
client_secret ="xxxxxxxxxxxxxxxxxxxx" 
refresh_token ="xxxxxxxxxxxxxxxxxxx" 
response  = oauth2a.RefreshToken(client_id,client_secret,refresh_token) 

def RefreshToken(client_id, client_secret, refresh_token): 
    params = {} 
    params['client_id'] = client_id 
    params['client_secret'] = client_secret 
    params['refresh_token'] = refresh_token 
    params['grant_type'] = 'refresh_token' 
    request_url = AccountsUrl('o/oauth2/token') 

    response = urllib.urlopen(request_url, urllib.urlencode(params)).read() 
    return json.loads(response) 

प्रतिक्रिया हमेशा {u'error ': u'invalid_grant'} होती है। मैं तीन अलग-अलग मशीनों पर इस कोशिश की है, और इसके अलावा जाओ HTTPError: HTTP त्रुटि 400: गलत अनुरोध

धन्यवाद

उत्तर

10

Invalid_grant त्रुटि दो सामान्य कारणों में है।

  1. आपके सर्वर की घड़ी NTP के साथ समन्वयित नहीं है। (समाधान: सर्वर का समय जांचें यदि यह गलत है।)
  2. रीफ्रेश टोकन सीमा पार हो गई है। (समाधान: आप कुछ भी नहीं कर सकते हैं, वे उपयोग में अधिक ताज़ा टोकन नहीं कर सकते हैं) अनुप्रयोग एकाधिक ताज़ा टोकन का अनुरोध कर सकते हैं। उदाहरण के लिए, यह ऐसी स्थितियों में उपयोगी है जहां उपयोगकर्ता एकाधिक मशीनों पर एक एप्लिकेशन इंस्टॉल करना चाहता है। इस मामले में, प्रत्येक ताज़ा करने के लिए दो ताज़ा टोकन की आवश्यकता होती है। जब ताज़ा टोकन की संख्या सीमा से अधिक हो जाती है, तो पुराने टोकन अमान्य हो जाते हैं। यदि एप्लिकेशन अमान्य रीफ्रेश टोकन का उपयोग करने का प्रयास करता है, तो एक अवैध_ग्रेंट त्रुटि प्रतिक्रिया लौटा दी जाती है। OAuth 2.0 क्लाइंट की प्रत्येक अद्वितीय जोड़ी की सीमा और 25 ताज़ा टोकन है (ध्यान दें कि यह सीमा बदल सकती है)। यदि एप्लिकेशन एक ही ग्राहक/खाता जोड़ी के लिए ताज़ा टोकन का अनुरोध जारी रखता है, तो 26 वें टोकन जारी होने के बाद, पहले जारी किए गए पहले ताज़ा टोकन को अमान्य कर दिया जाएगा। 27 वां अनुरोध किया गया ताज़ा टोकन दूसरे पहले जारी किए गए टोकन को अमान्य कर देगा और इसी तरह।
1

आप Google+ पायथन हाइब्रिड ऑथ नमूना में उपयोग किए गए दृष्टिकोण को अपनाने का प्रयास करना चाहते हैं और लाइब्रेरी को OAuth 2 टोकन प्रबंधित करने दें। एक पूर्ण उदाहरण यहाँ है:

https://developers.google.com/+/quickstart/python

कि नमूने में, कोड विनिमय (आप व्यवहार में एक DB का उपयोग करना चाहिए) गूगल की लाइब्रेरी का उपयोग किया जाता है और साख सफलता पर एक बुनियादी सत्र में जमा हो जाती है:

# Retrieve authorization code from POST data 
code = request.data 

try: 
    # Upgrade the authorization code into a credentials object 
    oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='') 
    oauth_flow.redirect_uri = 'postmessage' 
    credentials = oauth_flow.step2_exchange(code) 
except FlowExchangeError: 
    response = make_response(
    json.dumps('Failed to upgrade the authorization code.'), 401) 
    response.headers['Content-Type'] = 'application/json' 
    return response 

#... other code ... 
session['credentials'] = credentials 

बाद में, आप केवल प्रमाण-पत्र (सत्र या डीबी से) पुनर्प्राप्त कर सकते हैं और पाइथन लाइब्रेरी बाकी करता है।

"""Get list of people user has shared with this app.""" 
credentials = session.get('credentials') 
# Only fetch a list of people for connected users. 
if credentials is None: 
    response = make_response(json.dumps('Current user not connected.'), 401) 
    response.headers['Content-Type'] = 'application/json' 
    return response 
try: 
    # Create a new authorized API client. 
    http = httplib2.Http() 
    http = credentials.authorize(http) 
    # Get a list of people that this user has shared with this app. 
    google_request = SERVICE.people().list(userId='me', collection='visible') 
    result = google_request.execute(http=http) 

    response = make_response(json.dumps(result), 200) 
    response.headers['Content-Type'] = 'application/json' 
    return response 
except AccessTokenRefreshError: 
    response = make_response(json.dumps('Failed to refresh access token.'), 500) 
    response.headers['Content-Type'] = 'application/json' 
    return response 
1

मेरे पास एक ही समस्या (विंडोज़ ओएस) है। और मैंने तय किया है कि समय सर्वर सिंक्रनाइज़ेशन को अपडेट करके और यह अब पूरी तरह से काम करता है।

enter image description here

+0

लेकिन मेरे स्थानीय पर मैं यूटीसी का उपयोग +1 और उसके 14:25 लेकिन यूटीसी 13:25 है .. और मैं अभी भी अपने त्रुटि मिलती है। – Miguel

0

मैं ने वही समस्या थी और एक "invalid_grant" प्रतिक्रिया हो रही थी। बाहर निकलता है मैं grant_type = "client_credentials" के साथ प्रारंभिक OAuth कॉल कर रहा था और मुझे इसे grant_type = "password" का उपयोग करके बनाना चाहिए था। मेरे लिए काम करने वाले समाधान को साझा करना चाहता था।

grant_types के बीच अधिक विवरण नीचे

Difference between grant_type=client_credentials and grant_type=password in Authentication Flow?

0

पाया जा सकता है OAuth2 रीडायरेक्ट किसी ताज़ा टोकन सेट बिना वापस आ रहा था, मैं अपने समाधान मिल जाने इस सूत्र https://github.com/google/google-api-python-client/issues/213 में होना खोजने के बाद। संक्षेप, विनिमय प्रवाह के लिए prompt = 'consent' जोड़ें:

flow.params['prompt'] = 'consent' 
संबंधित मुद्दे