Title: Generate token for user programmatically
Last modified: October 31, 2019

---

# Generate token for user programmatically

 *  Resolved [crps92](https://wordpress.org/support/users/crps92/)
 * (@crps92)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/generate-token-for-user-programmatically/)
 * Hi,
 * with the plugin installed the logged in user has the option to generate a token
   for him/herself under “my profile” -> Open Authentication. (How) is it possible
   to archive this programmatically?
 * Thank you very much in advance!
 * **Background**
    – WP-Page and 1 Client – Client uses WP-REST API protected endpoints–
   user can log in (a) via WP-Page or (b) via Client – (b) works fine (User credentials
   flow)
 * The problem is that when the user logs in on the WP-page directly the Client 
   can not make requests on his behalf, so I want to generate the users token and
   send it to the Client along with some other information so that the following
   flow is identical no matter if the user initially came from the WP-page or the
   client

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [crps92](https://wordpress.org/support/users/crps92/)
 * (@crps92)
 * [6 years, 6 months ago](https://wordpress.org/support/topic/generate-token-for-user-programmatically/#post-12089829)
 * Found the solution, not sure if it’s the best one:
 *     ```
       function generate_oauth_token(){
           //if no token exists, create it
           if (is_null(wo_ap_et_access_token_for_user())){
               $_POST['generate_token']="something"; //$_POST['generate_token'] not being empty triggers token generation
               wo_user_profile_update_token_gen( null, null, null ); //$errors, $update, $user are all null
           }
           //get token and echo it
           $token = wo_ap_et_access_token_for_user();
           echo ($token -> access_token);
       }
       ```
   
 * functions wo_ap_et_access_token_for_user() and wo_user_profile_update_token_gen(
   $errors, $update, $user) can be found in includes/admin/profile.php
 *  Thread Starter [crps92](https://wordpress.org/support/users/crps92/)
 * (@crps92)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/generate-token-for-user-programmatically/#post-12263092)
 * seems like wo_ap_et_access_token_for_user return changed.
 * last line has to be
 * `echo ($token);`
    -  This reply was modified 6 years, 5 months ago by [crps92](https://wordpress.org/support/users/crps92/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Generate token for user programmatically’ is closed to new replies.

 * ![](https://ps.w.org/oauth2-provider/assets/icon-256x256.gif?rev=2603051)
 * [WP OAuth Server (OAuth Authentication)](https://wordpress.org/plugins/oauth2-provider/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/oauth2-provider/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/oauth2-provider/)
 * [Active Topics](https://wordpress.org/support/plugin/oauth2-provider/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/oauth2-provider/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/oauth2-provider/reviews/)

## Tags

 * [access token](https://wordpress.org/support/topic-tag/access-token/)

 * 2 replies
 * 1 participant
 * Last reply from: [crps92](https://wordpress.org/support/users/crps92/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/generate-token-for-user-programmatically/#post-12263092)
 * Status: resolved