{
    "mode": "perldoc",
    "parameter": "Net::Twitter::Role::OAuth",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3ATwitter%3A%3ARole%3A%3AOAuth/json",
    "generated": "2026-06-13T16:50:58Z",
    "synopsis": "use Net::Twitter;\nmy $nt = Net::Twitter->new(\ntraits          => ['API::RESTv11', 'OAuth'],\nconsumerkey    => \"YOUR-CONSUMER-KEY\",\nconsumersecret => \"YOUR-CONSUMER-SECRET\",\n);\n# Do some Authentication work. See EXAMPLES\nmy $tweets = $nt->friendstimeline;\nmy $res    = $nt->update({ status => \"I CAN HAZ OAUTH!\" });",
    "sections": {
        "NAME": {
            "content": "Net::Twitter::Role::OAuth - Net::Twitter role that provides OAuth instead of Basic\nAuthentication\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 4.01043\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::Twitter;\n\nmy $nt = Net::Twitter->new(\ntraits          => ['API::RESTv11', 'OAuth'],\nconsumerkey    => \"YOUR-CONSUMER-KEY\",\nconsumersecret => \"YOUR-CONSUMER-SECRET\",\n);\n\n# Do some Authentication work. See EXAMPLES\n\nmy $tweets = $nt->friendstimeline;\nmy $res    = $nt->update({ status => \"I CAN HAZ OAUTH!\" });\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Net::Twitter::Role::OAuth is a Net::Twitter role that provides OAuth authentication instead of\nthe default Basic Authentication.\n\nNote that this client only works with APIs that are compatible to OAuth authentication.\n",
            "subsections": []
        },
        "IMPORTANT": {
            "content": "Beginning with version 3.02, it is necessary for web applications to pass the \"callback\"\nparameter to \"getauthorizationurl\". In the absence of a callback parameter, when the user\nauthorizes the application a PIN number is displayed rather than redirecting the user back to\nyour site.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "See the \"examples\" directory in this distribution for working examples of both desktop and web\napplications.\n\nHere's how to authorize users as a desktop app mode:\n\nuse Net::Twitter;\n\nmy $nt = Net::Twitter->new(\ntraits          => ['API::RESTv11', 'OAuth'],\nconsumerkey    => \"YOUR-CONSUMER-KEY\",\nconsumersecret => \"YOUR-CONSUMER-SECRET\",\n);\n\n# You'll save the token and secret in cookie, config file or session database\nmy($accesstoken, $accesstokensecret) = restoretokens();\nif ($accesstoken && $accesstokensecret) {\n$nt->accesstoken($accesstoken);\n$nt->accesstokensecret($accesstokensecret);\n}\n\nunless ( $nt->authorized ) {\n# The client is not yet authorized: Do it now\nprint \"Authorize this app at \", $nt->getauthorizationurl, \" and enter the PIN#\\n\";\n\nmy $pin = <STDIN>; # wait for input\nchomp $pin;\n\nmy($accesstoken, $accesstokensecret, $userid, $screenname) = $nt->requestaccesstoken(verifier => $pin);\nsavetokens($accesstoken, $accesstokensecret); # if necessary\n}\n\n# Everything's ready\n\nIn a web application mode, you need to save the oauthtoken and oauthtokensecret somewhere\nwhen you redirect the user to the OAuth authorization URL.\n\nsub twitterauthorize : Local {\nmy($self, $c) = @;\n\nmy $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);\nmy $url = $nt->getauthorizationurl(callback => $callbackurl);\n\n$c->response->cookies->{oauth} = {\nvalue => {\ntoken => $nt->requesttoken,\ntokensecret => $nt->requesttokensecret,\n},\n};\n\n$c->response->redirect($url);\n}\n\nAnd when the user returns back, you'll reset those request token and secret to upgrade the\nrequest token to access token.\n\nsub twitterauthcallback : Local {\nmy($self, $c) = @;\n\nmy %cookie = $c->request->cookies->{oauth}->value;\nmy $verifier = $c->req->params->{oauthverifier};\n\nmy $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);\n$nt->requesttoken($cookie{token});\n$nt->requesttokensecret($cookie{tokensecret});\n\nmy($accesstoken, $accesstokensecret, $userid, $screenname)\n= $nt->requestaccesstoken(verifier => $verifier);\n\n# Save $accesstoken and $accesstokensecret in the database associated with $c->user\n}\n\nLater on, you can retrieve and reset those access token and secret before calling any Twitter\nAPI methods.\n\nsub maketweet : Local {\nmy($self, $c) = @;\n\nmy($accesstoken, $accesstokensecret) = ...;\n\nmy $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);\n$nt->accesstoken($accesstoken);\n$nt->accesstokensecret($accesstokensecret);\n\n# Now you can call any Net::Twitter API methods on $nt\nmy $status = $c->req->param('status');\nmy $res = $nt->update({ status => $status });\n}\n",
            "subsections": []
        },
        "METHODS": {
            "content": "authorized\nWhether the client has the necessary credentials to be authorized.\n\nNote that the credentials may be wrong and so the request may fail.\n",
            "subsections": [
                {
                    "name": "request_access_token",
                    "content": "Request the access token, access token secret, user id and screen name for this user. You\nmust pass the PIN# (for desktop applications) or the \"oauthverifier\" value, provided as a\nparameter to the oauth callback (for web applications) as $verifier.\n\nThe user must have authorized this app at the url given by \"getauthorizationurl\" first.\n\nReturns the accesstoken, accesstokensecret, userid, and screenname in a list. Also sets\nthem internally so that after calling this method, you can immediately call API methods\nrequiring authentication.\n"
                },
                {
                    "name": "xauth",
                    "content": "Exchanges the $username and $password for access tokens. This method has the same return\nvalue as \"requestaccesstoken\": accesstoken, accesstokensecret, userid, and screenname\nin a list. Also, like \"requestaccesstoken\", it sets the accesstoken and accesssecret,\ninternally, so you can immediately call API methods requiring authentication.\n"
                },
                {
                    "name": "get_authorization_url",
                    "content": "Get the URL used to authorize the user. Returns a \"URI\" object. For web applications, pass\nyour applications callback URL as the \"callback\" parameter. No arguments are required for\ndesktop applications (\"callback\" defaults to \"oob\", out-of-band).\n"
                },
                {
                    "name": "get_authentication_url",
                    "content": "Get the URL used to authenticate the user with \"Sign in with Twitter\" authentication flow.\nReturns a \"URI\" object. For web applications, pass your applications callback URL as the\n\"callback\" parameter. No arguments are required for desktop applications (\"callback\"\ndefaults to \"oob\", out-of-band).\n\naccesstoken\nGet or set the access token.\n\naccesstokensecret\nGet or set the access token secret.\n\nrequesttoken\nGet or set the request token.\n\nrequesttokensecret\nGet or set the request token secret.\n"
                }
            ]
        },
        "DEPRECATED METHODS": {
            "content": "oauth\nPrior versions used Net::OAuth::Simple. This method provided access to the contained\nNet::OAuth::Simple object. Beginning with Net::Twitter 3.00, the OAuth methods were\ndelegated to Net::OAuth::Simple. They have since made first class methods.\nNet::Simple::OAuth is no longer used. A warning will be displayed when accessing OAuth\nmethods via the <oauth> method. The \"oauth\" method will be removed in a future release.\n\nisauthorized\nUse \"authorized\" instead.\n\noauthauthorizationurl\nUse \"getauthorizationurl\" instead.\n\noauthtoken\n$nt->oauthtoken($accesstoken, $accesstokensecret);\n\nUse \"accesstoken\" and \"accesstokenseccret\" instead:\n\n$nt->accesstoken($accesstoken);\n$nt->accesstokensecret($accesstokensecret);\n",
            "subsections": []
        },
        "ACKNOWLEDGEMENTS": {
            "content": "This module was originally authored by Tatsuhiko Miyagawa as \"Net::Twitter::OAuth\", a subclass\nof the \"Net::Twitter\" 2.x. It was refactored into a Moose Role for use in \"Net::Twitter\" 3.0 and\nabove by Marc Mims. Many thanks to Tatsuhiko for the original work on both code and\ndocumentation.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Marc Mims <marc@questright.com>\n\nTatsuhiko Miyagawa <miyagawa@bulknews.net>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Net::Twitter, Net::Twitter::OAuth::Simple, Net::OAuth::Simple\n",
            "subsections": []
        }
    },
    "summary": "Net::Twitter::Role::OAuth - Net::Twitter role that provides OAuth instead of Basic Authentication",
    "flags": [],
    "examples": [
        "See the \"examples\" directory in this distribution for working examples of both desktop and web",
        "applications.",
        "Here's how to authorize users as a desktop app mode:",
        "use Net::Twitter;",
        "my $nt = Net::Twitter->new(",
        "traits          => ['API::RESTv11', 'OAuth'],",
        "consumerkey    => \"YOUR-CONSUMER-KEY\",",
        "consumersecret => \"YOUR-CONSUMER-SECRET\",",
        ");",
        "# You'll save the token and secret in cookie, config file or session database",
        "my($accesstoken, $accesstokensecret) = restoretokens();",
        "if ($accesstoken && $accesstokensecret) {",
        "$nt->accesstoken($accesstoken);",
        "$nt->accesstokensecret($accesstokensecret);",
        "unless ( $nt->authorized ) {",
        "# The client is not yet authorized: Do it now",
        "print \"Authorize this app at \", $nt->getauthorizationurl, \" and enter the PIN#\\n\";",
        "my $pin = <STDIN>; # wait for input",
        "chomp $pin;",
        "my($accesstoken, $accesstokensecret, $userid, $screenname) = $nt->requestaccesstoken(verifier => $pin);",
        "savetokens($accesstoken, $accesstokensecret); # if necessary",
        "# Everything's ready",
        "In a web application mode, you need to save the oauthtoken and oauthtokensecret somewhere",
        "when you redirect the user to the OAuth authorization URL.",
        "sub twitterauthorize : Local {",
        "my($self, $c) = @;",
        "my $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);",
        "my $url = $nt->getauthorizationurl(callback => $callbackurl);",
        "$c->response->cookies->{oauth} = {",
        "value => {",
        "token => $nt->requesttoken,",
        "tokensecret => $nt->requesttokensecret,",
        "},",
        "};",
        "$c->response->redirect($url);",
        "And when the user returns back, you'll reset those request token and secret to upgrade the",
        "request token to access token.",
        "sub twitterauthcallback : Local {",
        "my($self, $c) = @;",
        "my %cookie = $c->request->cookies->{oauth}->value;",
        "my $verifier = $c->req->params->{oauthverifier};",
        "my $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);",
        "$nt->requesttoken($cookie{token});",
        "$nt->requesttokensecret($cookie{tokensecret});",
        "my($accesstoken, $accesstokensecret, $userid, $screenname)",
        "= $nt->requestaccesstoken(verifier => $verifier);",
        "# Save $accesstoken and $accesstokensecret in the database associated with $c->user",
        "Later on, you can retrieve and reset those access token and secret before calling any Twitter",
        "API methods.",
        "sub maketweet : Local {",
        "my($self, $c) = @;",
        "my($accesstoken, $accesstokensecret) = ...;",
        "my $nt = Net::Twitter->new(traits => [qw/API::RESTv11 OAuth/], %param);",
        "$nt->accesstoken($accesstoken);",
        "$nt->accesstokensecret($accesstokensecret);",
        "# Now you can call any Net::Twitter API methods on $nt",
        "my $status = $c->req->param('status');",
        "my $res = $nt->update({ status => $status });"
    ],
    "see_also": []
}