Log on to Sina Weibo and get the token

Source: Internet
Author: User

Use WeiboAuthListener to obtain the code

Use the following code to obtain the token .. The sdks for semi-finished products are really urgent.


[Java]View plaincopy
[Java]View plaincopy
    1. String code = values. getString ("code ");
    2. If (code! = Null ){
    3. MText. setText ("Get authentication code: \ r \ n Code:" + code );
    4. Toast. makeText (MainActivity. this, "authentication code successful", Toast. LENGTH_SHORT). show ();
    5. }
    6. Try {
    7. URL url = new URL ("https://api.weibo.com/oauth2/access_token ");
    8. URLConnection connection = url. openConnection ();
    9. Connection. setDoOutput (true );
    10. OutputStreamWriter out = new OutputStreamWriter (connection
    11. . GetOutputStream (), "UTF-8 ");
    12. Out. write ("client_id = id & client_secret = secret & grant_type = authorization_code" +
    13. "& Code =" + code + "& redirect_uri = uri ");
    14. Out. flush ();
    15. Out. close ();
    16. String sCurrentLine;
    17. String sTotalString;
    18. SCurrentLine = "";
    19. STotalString = "";
    20. InputStream l_urlStream;
    21. Rochelle urlstream = connection. getInputStream ();
    22. BufferedReader l_reader = new BufferedReader (new InputStreamReader (
    23. Rochelle urlstream ));
    24. While (sCurrentLine = l_reader.readLine ())! = Null ){
    25. STotalString + = sCurrentLine;
    26. }
    27. JSONObject o = new JSONObject (sTotalString );
    28. System. out. println (o. getString ("access_token "));
    29. } Catch (Exception e ){
    30. // TODO Auto-generated catch block
    31. E. printStackTrace ();
    32. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.